我有一个全屏宽度的collectionview,但我希望collectionview和collectionview单元格能够根据iPhone屏幕调整大小。
一个单元格占据 Collection View 并滚动。
所以我希望 iPhone 4、4s、5 和 5s 的单元格和 Collection View 为 320 x 125; iPhone 6 为 375 x 146,iPhone 6 Plus 为 414 x 162。
无法在单元格上使用自动布局,但我可以在 Collection View 上使用自动布局。
感谢任何帮助。
谢谢
最佳答案
您可以实现单元格大小的委托(delegate)方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
您可以在那里返回您想要的尺寸。
从你的问题中我没有真正理解的是 Collection View 是否已经具有你想要的大小。如果是这样,在委托(delegate)方法中您可以执行以下操作:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(CGRectGetWidth(self.collectionView.frame), CGRectGetHeight(self.collectionView.frame));
}
这样单元格应该具有正确的大小。
希望这有帮助!
关于ios - xcode UICollectionView 单元格自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28361241/