UICollectionViewCell大小等于UICollectionView大小

时间:2016-07-29 04:53:28

标签: uicollectionview uicollectionviewcell uicollectionviewlayout

我想让UICollectionView Cell Size等于UICollectionView的宽度和高度。

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGSize size=collectionView.bounds.size;
    return CGSizeMake(414.0, 586.0);
}

如果我使用size,则会因为高度而抛出Autolayout错误the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values. 650.0;如果我将高度设置为586.0f

,它可以正常工作
UICollectionView frame = (0 0; 414 650); 
contentOffset: {0, 0}; contentSize: {0, 514}

那么,我该如何解决这个高度问题?

1 个答案:

答案 0 :(得分:1)

for iOS 9/10试试下一个UIController方法

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGSize scrollSize = self.collectionView.frame.size;
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    UIEdgeInsets inset =  layout.sectionInset;
    [layout setItemSize:CGSizeMake(scrollSize.width - inset.left - inset.right, scrollSize.height - inset.top - inset.bottom)];

    [self.collectionView.collectionViewLayout invalidateLayout];
}