首页 > 解决方案 > 如何在 tableviewcell 中进行高度约束?

问题描述

我在单元格中有表格视图、1 个单元格和 1 个视图。view 的约束:top,right,bottom,left 等于 superview height = 250 并且有错误。

class ViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.estimatedRowHeight = 250
    tableView.rowHeight = UITableView.automaticDimension
}


}

extension ViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    let view = UIView()

    view.translatesAutoresizingMaskIntoConstraints = false
    cell.contentView.addSubview(view)
    view.backgroundColor = UIColor.red
    view.heightAnchor.constraint(equalToConstant: 250).isActive = true
    view.topAnchor.constraint(equalTo: view.superview!.topAnchor).isActive = true
    view.bottomAnchor.constraint(equalTo: view.superview!.bottomAnchor).isActive = true
    view.leftAnchor.constraint(equalTo: view.superview!.leftAnchor).isActive = true
    view.rightAnchor.constraint(equalTo: view.superview!.rightAnchor).isActive = true
    return cell
}
}

我预计没有错误消息,但这里有。

2019-10-21 18:05:41.265530+0300 ffffffffff[73908:5577499] [LayoutConstraints] 无法同时满足约束。以下列表中的至少一个约束可能是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的;(2) 找到添加了一个或多个不需要的约束的代码并修复它。

"<NSLayoutConstraint:0x600002d9ea30 UIView:0x7feb2a700c00.height == 250   (active)>",
"<NSLayoutConstraint:0x600002d9e2b0 V:|-(0)-[UIView:0x7feb2a700c00]   (active, names:
'|':UITableViewCellContentView:0x7feb2a5050b0 )>",
"<NSLayoutConstraint:0x600002d9e1c0 UIView:0x7feb2a700c00.bottom == UITableViewCellContentView:0x7feb2a5050b0.bottom   (active)>",
"<NSLayoutConstraint:0x600002db8b90 'UIView-Encapsulated-Layout-Height'
UITableViewCellContentView:0x7feb2a5050b0.height == 250.333 (active)>" )

将尝试通过打破约束来恢复

在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点以在调试器中捕获它。中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能会有所帮助。

标签: iosswift

解决方案


推荐阅读