【问题标题】:UITableView automatic dimension not working: UIView-Encapsulated-Layout-Height errorUITableView 自动尺寸不起作用:UIView-Encapsulated-Layout-Height 错误
【发布时间】:2020-02-27 13:37:01
【问题描述】:

我正在尝试实现自我调整大小的 tableview 单元格。我在单元格内设置了自动布局高度,但它显示自动布局错误。

我设置 translatesAutoresizingMaskIntoConstraints = false 并设置 UITableView.autoDimension。

它显示如下自动布局错误。

[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:0x6000024ce9e0 DisposeSecond.MyCell:0x7f9ba7104aa0'cell'.height == 100   (active)>",
    "<NSLayoutConstraint:0x6000024ce800 'UIView-Encapsulated-Layout-Height' DisposeSecond.MyCell:0x7f9ba7104aa0'cell'.height == 100.333   (active)>"
)


我的实现很简单。


import UIKit

class MyCell: UITableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        translatesAutoresizingMaskIntoConstraints = false
        self.heightAnchor.constraint(equalToConstant: 100).isActive = true

        // Set debug colors to visualize heigh
        layer.borderWidth = 2
        layer.borderColor = UIColor.blue.cgColor
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class MyTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(MyCell.self, forCellReuseIdentifier: "cell")
    }


    // MARK: - Table view data source

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


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

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // I know it is set automatically but just indeed
        return UITableView.automaticDimension
    }
}

【问题讨论】:

    标签: swift uitableview autolayout uikit


    【解决方案1】:

    第一个问题:作为一般规则,您不应该使用以下内容修改 cell 视图本身:

        translatesAutoresizingMaskIntoConstraints = false
        self.heightAnchor.constraint(equalToConstant: 100).isActive = true
    

    改为修改单元格内容

       contentView.heightAnchor.constraint(equalToConstant: 100.0).isActive = true
    

    接下来,当使用自动调整大小的单元格和表格视图(或集合视图)时,自动布局在多次传递时经常会遇到冲突。这是由于分隔符、对象封装尺寸等原因造成的。

    您可以通过在约束上使用999priority 而不是默认的1000 来避免这种情况:

        // create heightAnchor for contentView
        let c = contentView.heightAnchor.constraint(equalToConstant: 100.0)
        // set priority to 999 to avoid auto-layout conflicts with auto-sizing cells
        c.priority = UILayoutPriority(rawValue: 999)
        // activate it
        c.isActive = true
    

    第三,在使用自动布局和自动调整单元格大小时,几乎不需要实现heightForRowAt。除非您特别了解为什么需要实施它,否则不要这样做。

    这是您的单元格类,已使用上述注释进行了修改:

    class MyCell: UITableViewCell {
    
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
    
            // never modify self in this way
            //translatesAutoresizingMaskIntoConstraints = false
            //self.heightAnchor.constraint(equalToConstant: 100).isActive = true
    
            // create heightAnchor for contentView
            let c = contentView.heightAnchor.constraint(equalToConstant: 100.0)
            // set priority to 999 to avoid auto-layout conflicts with auto-sizing cells
            c.priority = UILayoutPriority(rawValue: 999)
            // activate it
            c.isActive = true
    
            // Set debug colors to visualize heigh
            layer.borderWidth = 2
            layer.borderColor = UIColor.blue.cgColor
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 2017-02-01
      • 1970-01-01
      • 2015-10-09
      • 2016-03-04
      • 2021-09-01
      • 2021-05-21
      • 2021-01-14
      • 2014-06-12
      相关资源
      最近更新 更多