Ios 无法满足动态UITableViewCell的UILabel到ContentView的底部约束
所有实现都是以编程方式完成的(没有故事板!) 我已使用UITableView.automaticDimension和所有自定义单元格创建了动态表视图。所有单元格工作正常,此单元格也正常,但此单元格正在调试中生成约束警告 尽管如此,它的布局是完美的,并且显示得恰到好处 它只有一个标签和3层。以下是实施代码: //BadCustomTableViewCellIos 无法满足动态UITableViewCell的UILabel到ContentView的底部约束,ios,swift,uitableview,constraints,Ios,Swift,Uitableview,Constraints,所有实现都是以编程方式完成的(没有故事板!) 我已使用UITableView.automaticDimension和所有自定义单元格创建了动态表视图。所有单元格工作正常,此单元格也正常,但此单元格正在调试中生成约束警告 尽管如此,它的布局是完美的,并且显示得恰到好处 它只有一个标签和3层。以下是实施代码: //BadCustomTableViewCell override func layoutSubviews() { super.layoutSubviews() setUpVi
override func layoutSubviews() {
super.layoutSubviews()
setUpViews()
setUpConstraints()
}
let badLabel:UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 40, weight: UIFont.Weight.light)
label.text = "899"
label.textColor = .black
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func setUpViews() {
contentView.addSubview(badLabel)
}
func setUpConstraints() {
badLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
badLabel.layoutIfNeeded()
let safeMargin:CGFloat = badLabel.frame.size.width - 15
badLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: safeMargin).isActive = true
badLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -safeMargin).isActive = true
}
我认为一切都很好,但我不知道是什么打破了限制
日志显示:
(
"<NSLayoutConstraint:0x282729720 V:|-(77.6667)-[UILabel:0x103179a60'65.89 %'] (active, names: '|':UITableViewCellContentView:0x10317a150 )>",
"<NSLayoutConstraint:0x282729950 UILabel:0x103179a60'65.89 %'.bottom == UITableViewCellContentView:0x10317a150.bottom - 77.6667 (active)>",
"<NSLayoutConstraint:0x2827297c0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x10317a150.height == 48.6667 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x282729950 UILabel:0x103179a60'65.89 %'.bottom == UITableViewCellContentView:0x10317a150.bottom - 77.6667 (active)>
(
"",
"",
""
)
将尝试通过打破约束进行恢复
知道我在这里可能缺少什么吗?降低底部约束
let con = badLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -safeMargin)
con.priority = UILayoutPriority(999)
con.isActive = true
并在其中添加设置和约束
override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?)
作为
被多次调用
`虽然已经很晚了,但我还是要发布这个答案。大多数情况下,您不需要重新设置约束并将子视图重新添加到超级/父视图中。因此,您的setUpViews()
和setUpConstraints()
应该确实位于override init(style:UITableViewCell.CellStyle,reuseIdentifier:String?)内部,就像这样:
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpViews()
setUpConstraints()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
而且,你不需要(大多数时候也是如此),至少对我来说,为你的约束设置优先级。尽可能在不降低优先级的情况下解决约束问题/警告
badLabel.layoutifneed()
也不需要,除非您实际修改约束的某些常量或重新生成约束。如果您真的想在layoutSubviews()中设置所有内容,那么使用这样的行是有意义的
整个班级都像你的牢房一样为我工作:
class BadCustomTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpViews()
setUpConstraints()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let badLabel:UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 40, weight: UIFont.Weight.light)
label.text = "899"
label.textColor = .black
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func setUpViews() {
contentView.addSubview(badLabel)
}
func setUpConstraints() {
badLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
let safeMargin:CGFloat = badLabel.frame.size.width - 15
badLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: -safeMargin).isActive = true
badLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: safeMargin).isActive = true
}
}
您应用优先级的方法在layoutSubviews中对我有效,但在init()方法中无效。另外,由于我在单元格中使用了一些带有惰性变量和异步队列的动画,所以我更喜欢只使用layoutSubviews()。谢谢如果你的手机里有动画怎么办?我有它们,即使使用DispatchQueue.main.async()对我也不起作用
class BadCustomTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpViews()
setUpConstraints()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let badLabel:UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 40, weight: UIFont.Weight.light)
label.text = "899"
label.textColor = .black
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func setUpViews() {
contentView.addSubview(badLabel)
}
func setUpConstraints() {
badLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
let safeMargin:CGFloat = badLabel.frame.size.width - 15
badLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: -safeMargin).isActive = true
badLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: safeMargin).isActive = true
}
}