gpt4 book ai didi

swift - 以编程方式限制 UICollectionViewCell 的动态高度?

转载 作者:行者123 更新时间:2023-11-30 12:03:46 25 4
gpt4 key购买 nike

我从来没有见过比为 Collection View 单元格设置动态高度有如此多不同答案的主题。一些解决方案具有难以想象的代码量,这只会进一步加深人们的共识:UICollectionView 是一个集群,因为这可以通过 UITableView 来实现,而无需执行任何操作,因为动态单元格当约束被正确链接时,高度是内置的。但我想更好地处理 Collection View 。这是一场噩梦。

如何在 Swift 中以编程方式(无 Storyboard)将 Collection View 单元格的高度设置为其内容大小的动态,而无需一百行代码?

您可以将以下内容插入到一个空白项目中,如果您愿意的话,它将运行干净。

CollectionViewFlowLayout:

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

CollectionViewController

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

CollectionViewCell

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()
}
}

最佳答案

this的帮助下发布后,我能够以编程方式创建动态 UICollectionViewCell:

设置 UICollectionView 布局的estimatedItemSize,如下所示:

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSize(width: 1, height: 1)
}

在 UICollectionViewCell 的 contentView 中添加 UIControls

self.contentView.translatesAutoresizingMaskIntoConstraints = false

let screenWidth = UIScreen.main.bounds.size.width

contentView.addSubview(msgLabel)

[
msgLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
msgLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
msgLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
msgLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
msgLabel.widthAnchor.constraint(equalToConstant: screenWidth - 16)
].forEach{ $0.isActive = true }

Set numberOfLines of UILabel to 0

关于swift - 以编程方式限制 UICollectionViewCell 的动态高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46898538/

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