How to change UICollectionViewCell size programmatically in Swift?

前端 未结 6 1840
你的背包
你的背包 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: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

提交回复
热议问题