gpt4 book ai didi

ios - 在 swift 中使用约束的 UICollectionView 单元格固定宽度和动态高度

转载 作者:行者123 更新时间:2023-11-28 19:25:56 25 4
gpt4 key购买 nike

我有一个 collectionview 和一个基于其内容具有固定宽度和动态高度的单元格。我正在使用自动布局约束,我的代码如下。

我已经尝试在单元格和 View 中设置所有类型的自动约束。

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ViewControllerCell

cell.lbl_text.text = "djs sdfs sdfs dsfsfd gdgfd dfgd df "

return cell

}



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

if let flowLayout = coll_main?.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize

}



}

In cell class method:
override func awakeFromNib() {
super.awakeFromNib()
self.contentView.autoresizingMask = [UIView.AutoresizingMask.flexibleHeight]
}

override func layoutSubviews() {
self.layoutIfNeeded()
}

我得到了单元格宽度修复,但我没有设置那个宽度。这个问题如何解决?

这里是截图。

enter image description here enter image description here

最佳答案

您需要确保您的 CollectionView 的 Estimate Size 设置为 None

关于ios - 在 swift 中使用约束的 UICollectionView 单元格固定宽度和动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57900280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com