自动布局约束警告

时间:2018-10-09 14:00:24

标签: ios swift autolayout

因此,我正在以编程方式将视图添加到tableviewcell的内容视图中,并使用tableview提供的动态高度,并且我似乎正在收到以下自动布局警告,我担心,因为布局似乎没有被弄乱,但具有这些警告的控制台很烦人。

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2018-10-09 14:51:46.748606+0100 GenericForms[78197:5088471] [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:0x600001005fe0 UIView:0x7fa582d162e0.height == 55   (active)>",
    "<NSLayoutConstraint:0x600001018960 V:|-(20)-[UIView:0x7fa582d162e0]   (active, names: '|':UITableViewCellContentView:0x7fa582d15800 )>",
    "<NSLayoutConstraint:0x60000101c910 UIView:0x7fa582d162e0.bottom == UITableViewCellContentView:0x7fa582d15800.bottom - 20   (active)>",
    "<NSLayoutConstraint:0x60000103d950 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa582d15800.height == 95   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600001005fe0 UIView:0x7fa582d162e0.height == 55   (active)>

这是我用来在UITableViewCell内容视图内容中添加视图的代码。

if view == nil {
    view = UIView(frame: .zero)
    view?.backgroundColor = .green
    view?.translatesAutoresizingMaskIntoConstraints = false

    contentView.addSubview(view!)

    NSLayoutConstraint.activate([
        view!.heightAnchor.constraint(equalToConstant: 55),
        view!.topAnchor.constraint(equalTo: contentView.topAnchor, constant: edgeInsets.top),
        view!.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -edgeInsets.bottom),
        view!.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: edgeInsets.left),
        view!.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -edgeInsets.right)
        ])
}

在我的视图控制器中,这是我用来设置表格视图的代码。

class SelfSizedTableView: UITableView {
    var maxHeight: CGFloat = UIScreen.main.bounds.size.height

    override func reloadData() {
        super.reloadData()
        self.invalidateIntrinsicContentSize()
        self.layoutIfNeeded()
    }

    override var intrinsicContentSize: CGSize {
        let height = min(contentSize.height, maxHeight)
        return CGSize(width: contentSize.width, height: height)
    }
}
class FormViewController: UIViewController {

    private let tableView: SelfSizedTableView = {
       let tv = SelfSizedTableView()
        tv.isScrollEnabled = false
        tv.translatesAutoresizingMaskIntoConstraints = false
        tv.tableFooterView = UIView()
        tv.register(TestTableViewCell.self, forCellReuseIdentifier: "cellId")
        return tv
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .white
        tableView.dataSource = self
        view.addSubview(tableView)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        centerTableViewContent()
    }

    private func centerTableViewContent() {

        tableView.removeConstraints(tableView.constraints)

        if tableView.intrinsicContentSize.height > view.safeAreaLayoutGuide.layoutFrame.size.height {

            NSLayoutConstraint.activate([

                tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
                tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
                tableView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor),
                tableView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor)
                ])

        } else {

            NSLayoutConstraint.activate([

                tableView.heightAnchor.constraint(equalToConstant: tableView.intrinsicContentSize.height),
                tableView.widthAnchor.constraint(equalToConstant: view.frame.width),
                tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
                tableView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
                ])
        }
    }
}

extension FormViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! TestTableViewCell

        switch indexPath.row {
        case 0:
            cell.backgroundColor = .red
            cell.configure(with: "Item at: \(indexPath.row)", edgeInsets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
        case 1:
            cell.backgroundColor = .yellow
            cell.configure(with: "Item at: \(indexPath.row)", edgeInsets: UIEdgeInsets(top: 20, left: 10, bottom: 20, right: 10))
        default:
            cell.backgroundColor = .blue
            cell.configure(with: "Item at: \(indexPath.row)", edgeInsets: UIEdgeInsets(top: 70, left: 10, bottom: 70, right: 10))

        }

        return cell
    }
}

2 个答案:

答案 0 :(得分:6)

仅需分享:有一个great online tool可帮助您了解记录的错误消息。只需复制冲突约束的列表(包括周围的括号,请参见下文),然后将其粘贴到工具的文本区域中即可。然后,它将为您提供很好的可视化约束,通常可以帮助您理解问题所在。试试看!

复制并粘贴:

(
    "<NSLayoutConstraint:0x600001005fe0 UIView:0x7fa582d162e0.height == 55   (active)>",
    "<NSLayoutConstraint:0x600001018960 V:|-(20)-[UIView:0x7fa582d162e0]   (active, names: '|':UITableViewCellContentView:0x7fa582d15800 )>",
    "<NSLayoutConstraint:0x60000101c910 UIView:0x7fa582d162e0.bottom == UITableViewCellContentView:0x7fa582d15800.bottom - 20   (active)>",
    "<NSLayoutConstraint:0x60000103d950 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa582d15800.height == 95   (active)>"
)

答案 1 :(得分:2)

如果您正确阅读了警告,您将了解您正在单元格contentView中的视图上设置显式高度(55),并将其限制在{{ 1}}。因此,您基本上是在单元格内容上使用自动布局来指定单元格的高度。

为此,您应该将contentView设置为rowHeight,并将UITableViewAutomaticDimension设置为一个常数,以最佳估计其高度。

然后警告中的estimatedRowHeight约束告诉您,冲突是由于UIView-Encapsulated-Layout-Height根据tableView / {{1 }}-有关该主题的更多信息,请阅读this question

现在,您必须决定是否要使用通过rowHeight设置的行高,还是要使用对单元格内容进行自动布局约束所指定的高度。如果您想使用自动版式,则基于以下限制,请使用以下代码。

尝试使用此代码设置estimatedRowHeight

tableView.rowHeight

然后使用此代码来设置单元格的内容视图:

tableView