How to resize UICollectionViewCell in iPhone 6 and 6 Plus using autolayout

戏子无情 提交于 2019-12-06 05:17:23

问题


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)

        }


来源:https://stackoverflow.com/questions/27981200/how-to-resize-uicollectionviewcell-in-iphone-6-and-6-plus-using-autolayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!