UITableViewCell - 无法同时满足约束
Posted
技术标签:
【中文标题】UITableViewCell - 无法同时满足约束【英文标题】:UITableViewCell - unable to simultaneously satisfy constraints 【发布时间】:2018-07-14 17:36:11 【问题描述】:我正在使用 SnapKit 来进行约束并以编程方式创建视图。但是,有一点我无法理解。
我想在表格视图单元格的内容视图中添加一个子 UIView 并设置它的高度。这已经导致不可满足的约束错误。
我也尝试在表格视图中设置(估计的)rowHeight,但这没有帮助。
2018-07-14 19:21:38.785556+0200[20027:748731] [LayoutConstraints] 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.
(
"<SnapKit.LayoutConstraint:0x6000000b6500@WeatherCell.swift#24 UIView:0x7fa342409240.top == UITableViewCellContentView:0x7fa342413970.top>",
"<SnapKit.LayoutConstraint:0x6000000b7880@WeatherCell.swift#24 UIView:0x7fa342409240.bottom == UITableViewCellContentView:0x7fa342413970.bottom>",
"<SnapKit.LayoutConstraint:0x6000000b8a80@WeatherCell.swift#26 UIView:0x7fa342409240.height == 145.784842967987>",
"<NSLayoutConstraint:0x604000282210 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa342413970.height == 146 (active)>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:0x6000000b8a80@WeatherCell.swift#26 UIView:0x7fa342409240.height == 145.784842967987>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
单元格代码:
class WeatherCell: UITableViewCell
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)
let expandView = UIView()
self.contentView.addSubview(expandView)
expandView.snp.makeConstraints view in
view.edges.equalToSuperview()
view.height.equalTo(CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * 200)
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
【问题讨论】:
【参考方案1】:您需要引用底部约束并将其优先级更改为 999 / high 但不是 required ,因此删除边缘约束并分别制作它们
let expandView = UIView()
self.contentView.addSubview(expandView)
expandView.snp.makeConstraints (view) in
view.bottom.equalTo(self.contentView).priority(999) // can also be .priority(.high)
view.top.left.right.equalTo(self.contentView)
view.height.equalTo(CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * 200)
【讨论】:
在自动布局根据内容计算正确高度之前,它在日志UIView-Encapsulated-Layout Height UITableViewCellContentView:0x7fa342413970.height == 146
中假设一个静态高度,这肯定与当前内容高度冲突,因此这个日志,布局过程是递归,当您将底部约束的优先级设置为 none-required 时,此约束将被省略,直到自动布局最终确定并使其满足而不中断
@Sh_Khan 我遇到了类似的问题。如果我将底部约束的优先级降低到 999,我将不再收到冲突约束消息,但有时会在单元格之间出现死区。有什么想法吗?以上是关于UITableViewCell - 无法同时满足约束的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 uiimageview 同时满足 uitableviewcell 中的约束
关于单元重用的自定义 UITableViewCell 约束问题
iOS 8 UITableViewCell 与 UIImageView 动态高度