Placing a margin around each edge of the UICollectionView

后端 未结 7 1648
天涯浪人
天涯浪人 2020-12-14 18:18

When the UICollectionView is populated with items they always go right to the edges of the UICollectionView like so:

---------------
|X X X X X X X|
|X X X X         


        
7条回答
  •  庸人自扰
    2020-12-14 19:02

    I know this is an old question, but don't forget that UICollectionView inherits from UIScrollView.

    UICollectionView *collectionView = [[UICollectionView alloc] init];
    collectionView.contentInset = UIEdgeInsetsMake(x.x, x.x, x.x, x.x);
    
    // Swift 3 for good measure
    let collectionView = UICollectionView()
    collectionView.contentInset = UIEdgeInsets(top: x.x, left: x.x, bottom: x.x, right: x.x)
    

    Be aware that both contentInset and sectionInset may change the spacing of cells in your view. For more information on that, see this post.

提交回复
热议问题