简体   繁体   中英

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. 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 . .

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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