4

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

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

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.

Rugmangathan
  • 2,958
  • 6
  • 31
  • 42

1 Answers1

0

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)

        }
Ruso
  • 175
  • 5
  • 4
    This is completely incomprehensible. – Chris Conover Apr 21 '15 at 05:19
  • 1
    Sorry!!! If you want to make responsive ui this is the technique you have to use! In sizeForItemAtIndexPath you simply providing dynamic width and height for cell based on device screen size and number of items!!! If you don't understand that I'm sorry! – Ruso Apr 22 '15 at 00:01
  • The understandability issue is with your grammar. The question has nothing to do with feet, for example. Nor does your answer have anything to do with autolayout as asked in the question. You are bypassing auto layout and assigning a fixed size to every cell based on the size of the screen. The same functionality could be achieved more efficiently by simply setting itemSize to a fixed value after the collection view loads. – Eric Jul 28 '15 at 19:29
  • jejeje forget it mister! i just offering another solution that will deliver same result!!!! You want to use auto layout go ahead! – Ruso Jul 29 '15 at 00:05
  • I think it was a simple spelling mistake. He meant "fit" – Martin Lockett Sep 07 '15 at 13:30