How to resize UICollectionViewCell in iPhone 6 and

2020-07-18 02:53发布

问题:

I'm working in a iOS project where i use collectionView using Autolayout and SizeClasses. custom UICollectionViewCell xib is used. Where i reuse custom cell in collectionView presenting two cells like below image in iPhone 5s.

problem is while running the same project in iPhone 6 and 6 Plus, the cells are not resizing there is space between cells you can see it below.

which is resizing in xib when the size of cell is increased and select update frames but not resizing for iPhone 6.

Answers provided by other SO posts didn't helped.

回答1:

Math trick recall square root Number 33 = items count! You can tweak it independently from your total items count for sizing and feet more or less items in screen!!! For example / (Double(33)) on iPhone 4 will feet 33 items in 4 columns and 4 items per row! in iPhone 6 Pluse you will see same picture cells will scale up respecting 4 columns! If you put 1 you will get one cell full width on whatever device screen!

// ...........returning cell size based on device screen size by calculating square roo

        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

            let deviceSize = UIScreen.mainScreen().bounds.size
            let flexSize = sqrt(Double(deviceSize.width * deviceSize.height) / (Double(33)))

                return CGSize(width: flexSize , height: flexSize)

        }