Search code examples
iosswiftuicollectionviewswift4

How to show two columns in a CollectionView using swift4 in all devices


enter image description here

To show only two columns in a collectionView i am using this piece of code

    let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
    layout.minimumInteritemSpacing = 4
    layout.itemSize = CGSize(width:(self.collectionView.frame.size.width - 10)/2,height: (self.collectionView.frame.size.height)/3)

but it is only showing properly on 5s, not on every iphone. please help.

i want to show my view controller like this image. please help anyone

enter image description here


Solution

  • add this code to viewDidLoad. change height according to you.

    try this!

    if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout{
            layout.minimumLineSpacing = 10
            layout.minimumInteritemSpacing = 10
            layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
            let size = CGSize(width:(collectionView!.bounds.width-30)/2, height: 250)
            layout.itemSize = size
    }