How to display 3 cell per row UICollectionView

后端 未结 3 930
别跟我提以往
别跟我提以往 2021-01-27 05:30

As per the header, I\'m using UICollectionView to display images. I need to display 3 cells per row, something like how Instagram does it.

Because there are many screen

3条回答
  •  别那么骄傲
    2021-01-27 06:30

    Replace this line

    let width = (bounds.size.width - leftAndRightPaddings) / numberOfItemsPerRow
    

    with

    let width = (bounds.size.width - leftAndRightPaddings*4) / numberOfItemsPerRow
    

    As you are not considering spacing between two items & Right insets spacing therefore it is not adjusting in the screen.

    private let leftAndRightPaddings: CGFloat = 15.0
    private let numberOfItemsPerRow: CGFloat = 3.0
    
    let bounds = UIScreen.mainScreen().bounds
    let width = (bounds.size.width - leftAndRightPaddings*(numberOfItemsPerRow+1)) / numberOfItemsPerRow
    let layout = userDetailCollection.collectionViewLayout as! UICollectionViewFlowLayout
    layout.itemSize = CGSizeMake(width, width)   
    

    Try this code

提交回复
热议问题