How to resize UICollectionViewCell on iPhone 6 and 6 Plus using autorun

I work in an iOS project where I use collectionView using Autolayout and SizeClasses. uses custom UICollectionViewCell xib. Where I reuse a custom cell in a collectionView representing two cells, such as the image below in iPhone 5s. iPhone5s

a problem when starting the same project on the iPhone 6 and 6 Plus, the cells do not change the size of the space between the cells, which you can see below. iPhone 6

which resizes in xib when the cell size increases and selects update frames, but does not resize for iPhone 6.

Responses provided by other SO posts did not help.

+4
source share
1 answer

A mathematical trick calls the square root Number 33 = number of objects! You can configure it regardless of the total number of items for calibration and the legs of more or less items on the screen !!! For example, / (Double (33)) on iPhone 4 will process 33 items in 4 columns and 4 items in a row! in iPhone 6 Plus you will see that the same cameras will scale, observing 4 columns! If you put 1, you will get one full-width cell on any 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) } 
0
source

Source: https://habr.com/ru/post/980320/


All Articles