指定最小像元高度会产生警告吗?

时间:2018-11-16 18:33:47

标签: ios swift xcode uitableview constraints

我正在尝试为tableView单元格设置最小单元格高度。这行得通,但是它向Xcode的调试器控制台抛出了无数警告。

问题1 :为什么?

问题2 :如何在实现最小像元高度时摆脱这些警告?

2018-11-16 19:18:13.080131+0100 Test[46195:5492055] [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:0x600003a91450 V:[UILabel:0x7fa4f2e05470'One ']-(0)-|   (active, names: '|':UITableViewCellContentView:0x7fa4f2e17f70 )>",
    "<NSLayoutConstraint:0x600003a91310 V:|-(0)-[UILabel:0x7fa4f2e05470'One ']   (active, names: '|':UITableViewCellContentView:0x7fa4f2e17f70 )>",
    "<NSLayoutConstraint:0x600003a911d0 UILabel:0x7fa4f2e05470'One '.height >= 44   (active)>",
    "<NSLayoutConstraint:0x600003a8dbd0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa4f2e17f70.height == 44   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003a911d0 UILabel:0x7fa4f2e05470'One '.height >= 44   (active)>

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-11-16 19:18:13.081005+0100 Test[46195:5492055] [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:0x600003a953b0 V:[UILabel:0x7fa4f2c018d0'Three ']-(0)-|   (active, names: '|':UITableViewCellContentView:0x7fa4f2c06510 )>",
    "<NSLayoutConstraint:0x600003a95450 V:|-(0)-[UILabel:0x7fa4f2c018d0'Three ']   (active, names: '|':UITableViewCellContentView:0x7fa4f2c06510 )>",
    "<NSLayoutConstraint:0x600003a94be0 UILabel:0x7fa4f2c018d0'Three '.height >= 44   (active)>",
    "<NSLayoutConstraint:0x600003a95cc0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa4f2c06510.height == 44   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003a94be0 UILabel:0x7fa4f2c018d0'Three '.height >= 44   (active)>

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.

可以通过以下非常简单的设置轻松复制:

enter image description here

enter image description here

  • 带有控制器MyTableView的新UITableView
  • 1个具有MyTableViewCell类的原型单元格
  • 单元格有1个标签链接到MyTableViewCell.label

我对标签有以下限制:

enter image description here

这是为了确保我的最小像元高度为44。

视图控制器:

import UIKit

class MyTableViewController: UITableViewController {
    let data: [String] = [
    "One ",
    "Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two ",
    "Three "
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.estimatedRowHeight = 44.0
        tableView.rowHeight = UITableView.automaticDimension

    }

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

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! MyTableViewCell

        cell.label.text = data[indexPath.row]

        return cell
    }

}

单元格:

import UIKit

class MyTableViewCell: UITableViewCell {
    @IBOutlet weak var label: UILabel!
}

2 个答案:

答案 0 :(得分:1)

问题在于单元格中UILabel的高度> = 44约束。发生这种情况是因为UITableView自动向单元格contentView添加了height = 44约束。仅适用于带有OneThree标签的单元格,因为对于Two...标签,它使用标签的大小来计算行高。

但是单元格的contentView不仅包括标签,还包括默认高度为0.5的分隔符,因此默认单元格高度中的标签不是44,而是43.5,这就是约束被打破的原因。

因此,您需要使标签的高度约束> = 43.5,警告将消失。

答案 1 :(得分:1)

Peter Tretyakov的解决方案有效。

我发现的另一个解决方案是here

根据直觉,您需要降低优先级,将> =约束的优先级从1000设置为999,然后一切正常,而不会发出警告。