【发布时间】:2021-05-21 19:05:10
【问题描述】:
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// https://stackoverflow.com/a/27148268/14016301
tableView.estimatedRowHeight = 2.0
tableView.rowHeight = UITableView.automaticDimension
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = "AAAA"
let h = cell.contentView.heightAnchor.constraint(equalToConstant: 30)
h.isActive = true
_ = Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { _ in
UIView.animate(withDuration: 0.5) {
h.constant = 500
cell.layoutIfNeeded() // Layout issue here
}
})
return cell
}
}
我想知道为什么这段代码会产生布局问题:
2021-02-18 20:59:18.679060+0200 nvrGonnaGivYouApp[23688:813946] [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:0x600003c79400 UITableViewCellContentView:0x7fae0520c060.height == 500 (active)>",
"<NSLayoutConstraint:0x600003c78f50 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fae0520c060.height == 30.3333 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003c79400 UITableViewCellContentView:0x7fae0520c060.height == 500 (active)>
我尝试查看解决方案 here。但它没有帮助(如您所见,我从here 添加了解决方案。
我想要实现的是 100% 自动高度单元格。
谢谢
【问题讨论】:
标签: swift uitableview uikit tableview uitableviewautomaticdimension