How to set UICollectionViewCell Width and Height programmatically

后端 未结 20 1214
终归单人心
终归单人心 2020-12-04 08:06

I am trying to implement a CollectionView. When I am using Autolayout, my cells won\'t change the size, but their alignment.

Now I would rather want to

20条回答
  •  抹茶落季
    2020-12-04 08:56

    swift4 swift 4 ios collection view collectionview example xcode latest code working sample

    Add this on Delegate section of the top

    UICollectionViewDelegateFlowLayout

    and use this function

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (self.view.frame.size.width - 20) / 3 //some width
        let height = width * 1.5 //ratio
        return CGSize(width: width, height: height)
    }
    

    ///// sample complete code

    create on collection view and collectionview cell in storyboard give reference to the collection as
    @IBOutlet weak var cvContent: UICollectionView!

    paste this in View controller

     import UIKit
    
    class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
        var arrVeg = [String]()
        var arrFruits = [String]()
        var arrCurrent = [String]()
        
        @IBOutlet weak var cvContent: UICollectionView!
        
      
        
        override func viewDidLoad() {
            super.viewDidLoad()
            arrVeg = ["Carrot","Potato", "Tomato","Carrot","Potato", "Tomato","Carrot","Potato", "Tomato","Carrot","Potato", "Tomato"]
            
            arrVeg = ["Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange","Mango","Papaya","Orange"]
            
            
            arrCurrent = arrVeg
        }
        //MARK: - CollectionView
        
    
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let width = (self.view.frame.size.width - 20) / 3 //some width
            let height = width * 1.5 //ratio
            return CGSize(width: width, height: height)
        }
        
        func numberOfSections(in collectionView: UICollectionView) -> Int {
    
            return 1
        }
        
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            
            
            return arrCurrent.count
        }
        
        
        
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ContentCollectionViewCell
            cell.backgroundColor =  UIColor.green
            return cell
        }
    }
    

提交回复
热议问题