ios-如何在Swift中以编程方式更改UICollectionViewCell的大小?

我有一个UICollectionView和一个segmented control在数据之间切换。 但是,如何通过编程方式更改UICollectionViewCell的大小属性? 然后我可以为每种数据类型自定义每种样式。

例如,将宽度更改为520点,将高度更改为100。我是iOS开发的新手,并且对CSS:/有经验

我知道如何在Xcode中进行操作(显然),但是不知道迅速更改它的正确方法。 任何想法或反馈都非常感激(是)

enter image description here

5个解决方案
95 votes

确保您符合类的UICollectionViewDelegateFlowLayout(用于swift4)

eg. class MyClass: UICollectionViewDelegateFlowLayout

在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
    }

//快速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
}

//快速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
        }
    }

使用与上面针对swift 3相同的代码,但请确保您的类符合协议UICollectionViewDelegateFlowLayout

Sumit Oberoi answered 2020-02-12T17:44:35Z
61 votes

迅捷3

如果您使用的是Swift 3,则不会调用@ sumit-oberoi答案中的方法。要使其生效,请进行以下调整:

  1. 符合NSIndexPath协议。

  2. 实现这些方法:

    NSIndexPath

注意更改:

  1. 在方法名称中的IndexPath之前添加NSIndexPath
  2. NSIndexPath更改为IndexPath
JPetric answered 2020-02-12T17:45:26Z
33 votes

迅捷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()
}
Gaurav Rami answered 2020-02-12T17:45:46Z
6 votes

如果您使用Gaurav Rami的以下代码

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.setCollectionViewLayout(layout, animated: false)

然后消息将消失

Heo Đất Hades answered 2020-02-12T17:46:18Z
0 votes
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))
    }
Ananda Aiwale answered 2020-02-12T17:46:33Z
translate from https://stackoverflow.com:/questions/31662155/how-to-change-uicollectionviewcell-size-programmatically-in-swift