Uicollectionviewcontroller 3 columns auto layout

Asked

Viewed 70 times

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 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 don’t understand 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, my methods and library are in Objective-c tried to call some and worked, others not so much, so I preferred to continue with Objective-c.

thanks...

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
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.