我有UICollectionView,故事板中有单元格。每个单元格的大小设置为145x145。
它们在iPhone 4-5上看起来不错,但在iPhone 6和6+上,尺寸并没有按比例增加。与其手动为每个设备设置不同的单元大小,我还如何动态地进行呢?
参见这里的示例: iPhone 5S:http://prntscr.com/55vy7q,iPhone 6:http://prntscr.com/55vyy2
发布于 2014-11-13 10:13:50
如果希望单元格调整其宽度,则需要根据视图宽度计算流布局的itemSize。
itemSize属性:
#定义(UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;2 UICollectionViewFlowLayout *UICollectionViewFlowLayout CGFloat = availableWidthForCells CGFloat availableWidthForCells = CGRectGetWidth(self.collectionView.frame) - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumInteritemSpacing * (kCellsPerRow - 1);CGFloat cellWidth =availableWidthForCells/ kCellsPerRow;flowLayout.itemSize =CGSizeMake( kCellsPerRow,kCellsPerRow);
如果您想动态地改变每行单元格的数量,我已经提供了这些https://stackoverflow.com/a/36315107/4151918。collectionView:layout:sizeForItemAtIndexPath:委托中这样做。当设备方向发生变化时,重新计算itemSize并更新布局:
[self.collectionView.collectionViewLayout invalidateLayout][self.collectionView performBatchUpdates:nil completion:nil]https://stackoverflow.com/questions/26905191
复制相似问题