【发布时间】:2021-04-06 14:41:39
【问题描述】:
我的问题是为我的集合视图布局设置估计高度不起作用。具体来说,单元格不会动态调整大小。
我的假设是我的单元格上的 AutoLayout 约束设置不正确/UILabel
首先,我创建了一个自定义UICollectionViewCell
import UIKit
import SnapKit
class BubbleCell : UICollectionViewCell {
var label:UILabel!
var spacer:UIView!
var text:String = "" {
didSet {
guard label != nil else {return}
label.text = text
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupUI() {
label = UILabel()
label.textColor = .white
label.numberOfLines = 0
contentView.addSubview(label)
spacer = UIView()
contentView.addSubview(spacer)
label.snp.makeConstraints { (make) in
make.width.lessThanOrEqualToSuperview().multipliedBy(0.7)
make.top.right.equalTo(self.safeAreaLayoutGuide).inset(10)
}
spacer.snp.makeConstraints { (make) in
make.top.equalTo(self.label.snp.bottom)
make.left.right.bottom.equalToSuperview()
}
}
}
接下来,我创建了一个自定义UICollectionView。下面是我用来生成“布局”的函数
func getLayout() -> UICollectionViewCompositionalLayout {
let size = NSCollectionLayoutSize.init(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(200))
let item = NSCollectionLayoutItem.init(layoutSize: size)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitems: [item])
let section = NSCollectionLayoutSection.init(group: group)
return UICollectionViewCompositionalLayout.init(section: section)
}
现在,当我运行我的应用程序时,您可以看到单元格(红色背景)没有调整其高度以适应我的UILabel。我做错了什么?
【问题讨论】:
标签: ios swift user-interface autolayout snapkit