Placing a margin around each edge of the UICollectionView

后端 未结 7 1657
天涯浪人
天涯浪人 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 18:54

    You can make use of the following function.

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
      {
          return UIEdgeInsetsMake(50, 50,15,50);
      }
    

    You will have to play around with the number to figure out how to force the collectionviewCells in a single line.

    UIEdgeInsetsMake ( CGFloat top,CGFloat left,CGFloat bottom,CGFloat right); 
    

    For Swift 3

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(10, 4, 10, 4)
    }
    

提交回复
热议问题