问题描述
我UICollectionView在故事板细胞。每个小区的大小设置为145x145
I have UICollectionView with cells in the storyboard. The size of each cell is set to 145x145.
他们看起来对iPhone 4的好 - 5秒,但规模不会对iPhone 6和6+比例增加。而非手动设置不同的单元格大小为每个设备,我怎么能做到这一点动态?
They look good on the iPhone 4 - 5s, but the size doesn't increase proportionally on the iPhone 6 and 6+. Rather than manually setting a different cell size for each device, how can I do it dynamically?
见的例子在这里:iPhone 5S:,iPhone 6: http://prntscr.com/55vyy2
See examples here: iPhone 5S: http://prntscr.com/55vy7q, iPhone 6: http://prntscr.com/55vyy2
推荐答案
如果你想你的细胞来调整它们的宽度,你需要计算的流布局的 itemSize
基于视图的宽度。
If you want your cells to adjust their width, you'll need to calculate the flow layout's itemSize
based on the view width.
-
如果您所有的细胞会是同样大小,您可以设置流布局的
itemSize
属性:
If all your cells will be the same size, you can set the flow layout's
itemSize
property:
#define kCellsPerRow 2
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;
CGFloat availableWidthForCells = CGRectGetWidth(self.collectionView.frame) - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumInteritemSpacing * (kCellsPerRow - 1);
CGFloat cellWidth = availableWidthForCells / kCellsPerRow;
flowLayout.itemSize = CGSizeMake(cellWidth, flowLayout.itemSize.height);
的如果要动态地改变每个行单元格的数量,我提供这些 的
如果您需要计算每单元尺寸上,你可以做到这一点在的CollectionView:布局:sizeForItemAtIndexPath:
委托。
If you need to calculate per-cell sizes, you can do it in the collectionView:layout:sizeForItemAtIndexPath:
delegate.
重新计算itemSize当设备方向的变化和更新的布局:
Recalculate the itemSize when the device orientation changes and update the layout:
-
重绘布局无动画:
[self.collectionView.collectionViewLayout的invalidateLayout]
动画布局更改:
[self.collectionView performBatchUpdates:无完成:零]
这篇关于4.4和5.5英寸UICollectionViewCell动态调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!