UICollectionView spacing margins

后端 未结 15 1010
一向
一向 2020-11-30 16:58

I have a UICollectionView which shows photos. I have created the collectionview using UICollectionViewFlowLayout. It works good but I would like to

15条回答
  •  执笔经年
    2020-11-30 17:31

    In Objective-C

    CGFloat spacing = 5;
    UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout*)_habbitCollectionV.collectionViewLayout;
    flow.sectionInset = UIEdgeInsetsMake(0, spacing, 0, spacing);
    CGFloat itemsPerRow = 2;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat oneMore = itemsPerRow + 1;
    CGFloat width = screenRect.size.width - spacing * oneMore;
    CGFloat height = width / itemsPerRow;
    
    flow.itemSize = CGSizeMake(floor(height), height);
    flow.minimumInteritemSpacing = spacing;
    flow.minimumLineSpacing = spacing;
    

    All you have to do is change the itemsPerRow value and it will update the number of items per row accordingly. Furthermore, you can change the spacing value if you want more or less general spacing.

提交回复
热议问题