gpt4 book ai didi

swift - 使用自动布局以编程方式设置 UITableViewCell 高度

转载 作者:行者123 更新时间:2023-11-30 10:42:32 25 4
gpt4 key购买 nike

我想使用自动布局设置单元格高度,代码如下:

class AccountsBalanceResidueCell : BaseCell {

private let title: UILabel = {
let lbl = LabelSL.italicLarge()
lbl.text = Strings.Common.accountsResidue.value
return lbl
}()

private let amount: UILabel = LabelSL.italicLarge()

func setup(_ amount: String) {
self.amount.text = amount + " " + Strings.Common.roubleSymbol.value
}

override func prepare() {
contentView.backgroundColor = .cyan
contentView.translatesAutoresizingMaskIntoConstraints = false
addSubview(title)
addSubview(amount)
setConstraints()
}

private func setConstraints(){
let height: CGFloat = UIScreen.main.bounds.size.height * 0.3333

contentView.heightAnchor.constraint(equalToConstant: height).isActive = true
}
}

但是,我在日志中收到错误:

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( " (active)>", " (active)>" )

Will attempt to recover by breaking constraint (active)>

最佳答案

通过添加辅助 View 解决了这个问题:

private let container: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .clear
return view
}()

container.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
container.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
container.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
container.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
let heightConstraint = container.heightAnchor.constraint(equalToConstant: height)
heightConstraint.priority = UILayoutPriority(rawValue: 751)
heightConstraint.isActive = true

关于swift - 使用自动布局以编程方式设置 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56473837/

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