UICollectionViewController 3 columns auto layout

0

I'm creating a UICollectionViewController with 2 or 3 columns, when using autolayout in some Devices works perfectly but in others not, I did not find anything that speaks of how to dynamize these columns with objective-c.

I found one in swift, which has this calculation.

private let leftAndRightPaddings: CGFloat = 30.0
private let numberOfItemsPerRow: CGFloat = 2.0
private let heigthAdjustment: CGFloat = 5.0

let width = (CGRectGetWidth(collectionView!.frame) - leftAndRightPaddings) / numberOfItemsPerRow
let layout = collectionViewLayout as! UICollectionViewFlowLayout  
layout.itemSize = CGSizeMake(width, width + heigthAdjustment)

I do not understand anything what this code is doing down here

let layout = collectionViewLayout as! UICollectionViewFlowLayout

With this reiderization code is working with Swift but I can not use it, my methods and library are in Objective-c I tried to call some and worked, others not so much, so I preferred to continue with Objective-c.

Thanks for your help.

    
asked by anonymous 13.07.2016 / 01:16

1 answer

3

Try this.

MyCollectionView.swift

    self.collectionView.delegate = self

    self.collectionView.dataSource = self

    collectionViewLayout = CustomFlowLayout()

    collectionView.collectionViewLayout = collectionViewLayout

CustomFlowLayout.swift

class CustomFlowLayout: UICollectionViewFlowLayout {

override init() {

    super.init()

    setupLayout()
}

required init?(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)

    setupLayout()
}

override var itemSize: CGSize {
    set {

    }
    get {
        let numberOfColumns: CGFloat = 3

        let itemWidth = (CGRectGetWidth(self.collectionView!.frame) - (numberOfColumns - 1)) / numberOfColumns

        return CGSizeMake(itemWidth, itemWidth)
    }
}

func setupLayout() {

    minimumInteritemSpacing = 1

    minimumLineSpacing = 1

    scrollDirection = .Vertical
}

}

    
13.07.2016 / 03:59