所有实现均以编程方式完成(没有情节提要!)。
我已经使用所有自定义单元格的UITableView.automaticDimension创建了动态tableView 。所有单元都工作正常,并且 此 也一样,但是 此 一个正在生成约束警告在调试中。
尽管如此,布局还是完美的,并且可以按原样显示。
它只有一个标签和3个CAShapeLayers。下面是实现代码:
// BadCustomTableViewCell
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)>
知道我在这里可能会缺少什么吗?
答案 0 :(得分:1)
降低底部约束
let con = badLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -safeMargin)
con.priority = UILayoutPriority(999)
con.isActive = true
并在其中添加设置和约束
override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?)
为
override func layoutSubviews()
被多次调用 `
答案 1 :(得分:0)
虽然已经很晚了,但我还是要发布此答案。在大多数情况下,您无需重新设置约束并将子视图重新添加到上级/父级视图中。因此,您的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.layoutIfNeeded()
。如果您真的想在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
}
}