UICollectionView Set number of columns

后端 未结 17 1173
执笔经年
执笔经年 2020-11-28 17:28

I just started learning about UICollectionViews. I\'m wondering if anyone knows how to specify the number of columns in a collectionview. The default is set to 3 (iPhone/por

17条回答
  •  借酒劲吻你
    2020-11-28 18:14

    Its all about layout you want to draw. You can create custom class inheriting from UICollectionViewFlowLayout. Currently there is not any direct method to set columns. If you want to achieve this kind of functionality you need to do it manually. You need to handle it in your custom flow layout class.

    Now question will arise how you will do it? If you dont want to disturb cell frame you can adjust

     collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
     collectionView:layout:minimumLineSpacingForSectionAtIndex:
    

    Another way is provide you own positions of cells. By overriding below two methods, which will get called during your layout formation.

      - (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
      - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
    

    UICollectionViewLayoutAttributes is class which will deal with cell position, frame, Zindex etc

提交回复
热议问题