如何使用自动布局在 iPhone 6 和 6 Plus 中调整 UICollectionViewCell 的大小 [英] How to resize UICollectionViewCell in iPhone 6 and 6 Plus using autolayout

查看:21
本文介绍了如何使用自动布局在 iPhone 6 和 6 Plus 中调整 UICollectionViewCell 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 iOS 项目中工作,在该项目中我使用了使用 Autolayout 和 SizeClasses 的 collectionView.使用自定义 UICollectionViewCell xib.我在 collectionView 中重用自定义单元格在 iPhone 5s 中呈现两个单元格,如下图所示.

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.

问题是在 iPhone 6 和 6 Plus 中运行同一个项目时,单元格没有调整大小,单元格之间有空间,您可以在下面看到它.

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.

当单元格的大小增加时在xib中调整大小并选择更新帧但不调整iPhone 6的大小.

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

其他 SO 帖子提供的答案没有帮助.

Answers provided by other SO posts didn't helped.

推荐答案

数学技巧回忆平方根数字 33 = 物品计数!您可以独立于您的总项目数进行调整,以进行尺寸调整和在屏幕上显示更多或更少的项目!!!例如,iPhone 4 上的/(Double(33)) 将在 4 列中包含 33 个项目,每行包含 4 个项目!在 iPhone 6 Pluse 中,您会看到相同的图片单元会按 4 列放大!如果您输入 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)

        }

这篇关于如何使用自动布局在 iPhone 6 和 6 Plus 中调整 UICollectionViewCell 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆