How to change UICollectionViewCell size programmatically in Swift?

前端 未结 6 1829
你的背包
你的背包 2020-12-07 20:09

I have a UICollectionView with a segmented control to switch between data. But how do I change the UICollectionViewCell size propertie

相关标签:
6条回答
  • 2020-12-07 20:29

    Try to use UICollectionViewDelegateFlowLayout method. In Xcode 11 or later, you need to set Estimate Size to none from storyboard.

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: 
    UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      let padding: CGFloat =  170
      let collectionViewSize = advertCollectionView.frame.size.width - padding
      return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
    }
    
    0 讨论(0)
  • 2020-12-07 20:30

    Swift 3

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let cellSize = CGSize(width:80 , height:80)
    
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical //.horizontal
        layout.itemSize = cellSize
        layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
        layout.minimumLineSpacing = 1.0
        layout.minimumInteritemSpacing = 1.0
        myCollectionView.setCollectionViewLayout(layout, animated: true)
    
        myCollectionView.reloadData()
    }
    
    0 讨论(0)
  • 2020-12-07 20:43

    Make sure you conform to UICollectionViewDelegateFlowLayout for your class (for swift4)

    eg. class MyClass: UICollectionViewDelegateFlowLayout
    

    Prior to Swift 3

    //Use for size
    func collectionView(collectionView: UICollectionView,
            layout collectionViewLayout: UICollectionViewLayout,
            sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    
        }
    //Use for interspacing
        func collectionView(collectionView: UICollectionView,
            layout collectionViewLayout: UICollectionViewLayout,
            minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
                return 1.0
        }
    
        func collectionView(collectionView: UICollectionView, layout
            collectionViewLayout: UICollectionViewLayout,
            minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
                return 1.0
        }
    

    // For swift 3

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
    
    }
    
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout
        collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }
    

    // For swift 4

    extension MyClass: UICollectionViewDelegateFlowLayout { 
            func collectionView(_ collectionView: UICollectionView,
                                layout collectionViewLayout: UICollectionViewLayout,
                                sizeForItemAt indexPath: IndexPath) -> CGSize {
    
            }
    
            func collectionView(_ collectionView: UICollectionView,
                                layout collectionViewLayout: UICollectionViewLayout,
                                minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
                return 1.0
            }
    
            func collectionView(_ collectionView: UICollectionView, layout
                collectionViewLayout: UICollectionViewLayout,
                                minimumLineSpacingForSectionAt section: Int) -> CGFloat {
                return 1.0
            }
        }
    

    use the same code as shown above for swift 3 but just make sure your class conforms to protocol UICollectionViewDelegateFlowLayout

    0 讨论(0)
  • 2020-12-07 20:49

    Swift 3

    Methods from @sumit-oberoi answer won't be called if you are using Swift 3. To make it work make this adjustments:

    1. Conform to the UICollectionViewDelegateFlowLayout protocol.

    2. Implement these methods:

      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          sizeForItemAt indexPath: IndexPath) -> CGSize {
      
      }
      
      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
          return 1.0
      }
      
      func collectionView(_ collectionView: UICollectionView, layout
          collectionViewLayout: UICollectionViewLayout,
                          minimumLineSpacingForSectionAt section: Int) -> CGFloat {
          return 1.0
      }
      

    Notice the changes:

    1. add _ before collectionView in the method name
    2. NSIndexPath changes to IndexPath
    0 讨论(0)
  • 2020-12-07 20:53
    first Conform protocol the UICollectionViewDelegateFlowLayout
    
    set width and height  UICollectionViewCell 
    
    UIcollectionViewCell Hight = 300;
    UIcollectionViewCell Width = 380;
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
          return CGSize(width: CGFloat((380)), height: CGFloat(300))
        }
    
    0 讨论(0)
  • 2020-12-07 20:55

    if you use Gaurav Rami's code bellow

    let cellSize = CGSize(width:80 , height:80)
    
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical //.horizontal
    layout.itemSize = cellSize
    layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
    layout.minimumLineSpacing = 1.0
    layout.minimumInteritemSpacing = 1.0
    myCollectionView.setCollectionViewLayout(layout, animated: true)
    

    and get this message "Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates." you should remove animated when setting view layout for collection view

    myCollectionView.setCollectionViewLayout(layout, animated: false)
    

    then the message will disappear

    0 讨论(0)
提交回复
热议问题