我正在尝试摆脱自定义单元格在UITableView
中出现的约束问题。行的高度设置为自动,并由约束条件定义。
例如,我有一个仅包含UITextField
的单元格,该单元格的高度设置为(44.0),并且在顶部和底部粘着。
当我的UITableView
上的分隔符被停用时,没有问题。因此,当我激活它们时,控制台中会显示以下警告:
[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.
(
"<NSLayoutConstraint:0x28333a670 UITextField:0x103655390.height == 44 (active)>",
"<NSLayoutConstraint:0x28333a7b0 V:[UITextField:0x103655390]-(0)-| (active, names: '|':UITableViewCellContentView:0x103654e20 )>",
"<NSLayoutConstraint:0x28333a990 V:|-(0)-[UITextField:0x103655390] (active, names: '|':UITableViewCellContentView:0x103654e20 )>",
"<NSLayoutConstraint:0x28333ba20 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x103654e20.height == 44.3333 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x28333a670 UITextField:0x103655390.height == 44 (active)>
似乎系统添加了UIView-Encapsulated-Layout-Height
约束来设置高度,并且其中包含分隔符的大小(0.33)。它与所需的高度冲突,并且它覆盖了我的约束。
例如,当我尝试通过更改UITextField
的高度来更新单元格的高度时,这是一个问题。
有什么方法可以消除那些烦人的警告?
感谢您,我找到了一种消除警告并在运行时获取动画高度转换的方法:
1:设置底部约束优先级≤999,
2:更改约束后,致电
tableView.beginUpdates()
tableView.endUpdates()
这将重新计算行的高度,UITableView
自动为更改添加动画效果。
BUT动画出现故障...
如果将底部约束优先级设为1000,则动画是平滑的,但是每次约束更改时都会出现警告。
例如,如果将最低约束优先级设置为999,警告就会消失,但是单元格的内容会从一个位置跳到另一位置...
我将暂时保留警告,并希望苹果在将来的版本中解决此问题。
谢谢您的回答!
@ blacKeuz在UITableViewDataSource的CellForRowAtIndexPath上尝试以下操作
let cell = tableView.dequeueReusableCell(withIdentifier: YourCustomTableViewCell.reuseIdentifier, for: indexPath) as! YourCustomTableViewCell
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
cell.setNeedsLayout()
cell.layoutIfNeeded()
return cell