在 UIViewAlertForUnsatisfiableConstraints 中使用 Autolayout 结果的可扩展 UITableView Cell

Mil*_*sáľ 2 uitableview ios autolayout swift

我正在尝试使用UITableViewAutomaticDimension行高和单元格自动布局的组合来制作可扩展单元格。为了让事情变得简单,我从以下简单的代码开始,我希望它可以工作:

import UIKit

class ViewController: UITableViewController {

    let cells = [Cell(), Cell(), Cell()]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 50

        for cell in cells {
            cell.tableView = tableView
        }
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return cells[indexPath.row]
    }
}

class Cell: UITableViewCell {

    var cons: NSLayoutConstraint?
    var tableView: UITableView?

    init() {
        super.init(style: .default, reuseIdentifier: nil)

        let button = UIButton()
        contentView.addSubview(button)
        button.addTarget(self, action: #selector(Cell.tapped(_:)), for: .touchUpInside)
        button.setTitle("Press me", for: .normal)
        button.setTitleColor(.red, for: .normal)

        button.translatesAutoresizingMaskIntoConstraints = false

        // constraining the button to the view, so that buttons height would define cell height
        let constraints = [
            button.topAnchor.constraint(equalTo: contentView.topAnchor),
            button.rightAnchor.constraint(equalTo: contentView.rightAnchor),
            button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
            button.leftAnchor.constraint(equalTo: contentView.leftAnchor),
        ]

        NSLayoutConstraint.activate(constraints)
        cons = button.heightAnchor.constraint(equalToConstant: 100)
        cons?.isActive = true
    }

    func tapped(_ sender: UIButton) {
        // change the height of the button, thus of the contentView too (since the button is constrained to the contentView)
        if cons?.constant == 100 {
            cons?.constant = 200
        } else {
            cons?.constant = 100
        }
        // tell the tableView to redraw itself to apply the change
        tableView?.beginUpdates()
        tableView?.setNeedsDisplay()
        tableView?.endUpdates()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Run Code Online (Sandbox Code Playgroud)

现在这似乎有效,但是,我仍然收到UIViewAlertForUnsatisfiableConstraints以下消息的警告:

(
"<NSLayoutConstraint:0x17408c7b0 V:|-(0)-[UIButton:0x109e10290'Press me']   (active, names: '|':UITableViewCellContentView:0x109e0fd90 )>",
"<NSLayoutConstraint:0x17408c8a0 UIButton:0x109e10290'Press me'.bottom == UITableViewCellContentView:0x109e0fd90.bottom   (active)>",
"<NSLayoutConstraint:0x17408c990 UIButton:0x109e10290'Press me'.height == 200   (active)>",
"<NSLayoutConstraint:0x17408db10 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x109e0fd90.height == 100   (active)>"
)

 Will attempt to recover by breaking constraint 
 <NSLayoutConstraint:0x17408c990 UIButton:0x109e10290'Press me'.height == 200   (active)>
Run Code Online (Sandbox Code Playgroud)

估计(或当前)行高似乎与我提供的自动布局约束相冲突。尽管如此,使用以下代码刷新 tableView 将按预期显示它:

tableView?.beginUpdates()
tableView?.setNeedsDisplay()
tableView?.endUpdates()
Run Code Online (Sandbox Code Playgroud)

我如何删除警告?

我知道简单地将高度约束的优先级更改为 999 将删除警告,但在我看来,删除它是一种技巧,而不是解决方案。如果我错了,请解释。谢谢。

Don*_*Mag 6

将高度约束优先级设置为 999 可能感觉像是一个黑客,但根据 Apple 的文档:

注意不要觉得有义务使用所有 1000 个优先级值。事实上,优先级通常应围绕系统定义的低 (250)、中 (500)、高 (750) 和必需 (1000) 优先级聚集。您可能需要制定比这些值高或低一或两个点的约束,以帮助防止平局。如果您想超出此范围,您可能需要重新检查您的布局逻辑。

可能有点不直观:

“我想让按钮高度控制单元格高度,所以降低它的优先级????”

但是,这似乎是做到这一点的“正确”方式。

参考:https : //developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/AnatomyofaConstraint.html#//apple_ref/doc/uid/TP40010853-CH9-SW19