UICollectionView Set number of columns

后端 未结 17 1170
执笔经年
执笔经年 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:13

    CollectionViews are very powerful, and they come at a price. Lots, and lots of options. As omz said:

    there are multiple ways you could change the number of columns

    I'd suggest implementing the Protocol, giving you access to the following methods in which you can have greater control over the layout of your UICollectionView, without the need for subclassing it:

    • collectionView:layout:insetForSectionAtIndex:
    • collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
    • collectionView:layout:minimumLineSpacingForSectionAtIndex:
    • collectionView:layout:referenceSizeForFooterInSection:
    • collectionView:layout:referenceSizeForHeaderInSection:
    • collectionView:layout:sizeForItemAtIndexPath:

    Also, implementing the following method will force your UICollectionView to update it's layout on an orientation change: (say you wanted to re-size the cells for landscape and make them stretch)

    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                   duration:(NSTimeInterval)duration{
    
        [self.myCollectionView.collectionViewLayout invalidateLayout];
    }
    

    Additionally, here are 2 really good tutorials on UICollectionViews:

    http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

    http://skeuo.com/uicollectionview-custom-layout-tutorial

提交回复
热议问题