简体   繁体   English

相对于屏幕尺寸快速设置收藏夹视图单元格的尺寸

[英]Set collection view cell size relative to screen size swift

I am Trying to resize my cell based on the screen size so that there is only 2 cell per row. 我正在尝试根据屏幕大小调整单元格的大小,以便每行只有2个单元格。 I am using the code 我正在使用代码

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
       return CGSize(width: MyCollectionView.bounds.width / 2.2, height: MyCollectionView.bounds.height / 5.5)    
}

This works and resizes to the different screen sizes. 这可以工作并调整为不同的屏幕尺寸。 I run it on. 我继续 however when you rotate into landscape mode it displays more than two cells per row. 但是,当您旋转到横向模式时,每行显示两个以上的单元格。

If The container of MyCollectionView is self.view then plz give a try to this . 如果MyCollectionView的容器是self.view,则请尝试尝试this。 .

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: self.view.frame.width / 2.2 , height: MyCollectionView.bounds.height / 5.5)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM