我从来没有见过一个主题有这么多不同的答案,而不是为集合视图单元格设置动态高度。一些解决方案具有难以想象的代码量,这只会进一步促成UICollectionView
集群的共识,因为这可以通过UITableView
不做任何事情来实现,因为当约束被正确链接时,动态单元高度是内置的。但我想更好地处理集合视图。这是一场噩梦。
如何在没有一百行代码的情况下在 Swift 中以编程方式(无情节提要)将集合视图单元格的高度设置为动态到其内容大小?
您可以将以下内容插入一个空白项目,如果您愿意,它将运行干净。
集合视图流布局:
class CollectionViewFlowLayout: UICollectionViewFlowLayout {
// inits
override init() {
super.init()
config()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// config
func config() {
scrollDirection = .vertical
minimumLineSpacing = 0
minimumInteritemSpacing = 0
}
}
集合视图控制器
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
// model properties
let colorArray: [UIColor] = [.red, .green, .blue, .yellow]
let labelArray: [String] = [
"sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf",
"sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf",
"sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf",
"sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf sconewolf"
]
// view did load
override func viewDidLoad() {
super.viewDidLoad()
config()
}
// config
func config() {
collectionView?.dataSource = self
collectionView?.delegate = self
collectionView?.backgroundColor = UIColor.gray
collectionView?.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)
collectionView?.alwaysBounceVertical = true
collectionView?.alwaysBounceHorizontal = false
collectionView?.register(CollectionViewCell.self, forCellWithReuseIdentifier: "cell1")
}
// data source: number of items in section
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return colorArray.count
}
// data source: cell for item
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! CollectionViewCell
cell.backgroundColor = colorArray[indexPath.item]
cell.label.text = labelArray[indexPath.item]
return cell
}
// delegate: size for item
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 200)
}
}
集合视图单元
class CollectionViewCell: UICollectionViewCell {
// view properties
var label = UILabel()
// inits
override init(frame: CGRect) {
super.init(frame: frame)
config()
addLabel()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// config
func config() {
self.translatesAutoresizingMaskIntoConstraints = false
}
// add label
func addLabel() {
label.font = UIFont.boldSystemFont(ofSize: 18)
label.textColor = UIColor.black
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
label.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
label.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
label.sizeToFit()
}
}