【问题标题】:UIView-Encapsulated-Layout-Height constraint issue in Table View表视图中的 UIView-Encapsulated-Layout-Height 约束问题
【发布时间】:2021-05-21 19:05:10
【问题描述】:
class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // https://stackoverflow.com/a/27148268/14016301
        
        tableView.estimatedRowHeight = 2.0
        tableView.rowHeight = UITableView.automaticDimension
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        
        cell.textLabel?.text = "AAAA"
        let h = cell.contentView.heightAnchor.constraint(equalToConstant: 30)
        h.isActive = true
        
        _ = Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { _ in
            UIView.animate(withDuration: 0.5) {
                h.constant = 500
                cell.layoutIfNeeded() // Layout issue here
            }
        })
        
        return cell
    }

}

我想知道为什么这段代码会产生布局问题:

2021-02-18 20:59:18.679060+0200 nvrGonnaGivYouApp[23688:813946] [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:0x600003c79400 UITableViewCellContentView:0x7fae0520c060.height == 500   (active)>",
    "<NSLayoutConstraint:0x600003c78f50 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fae0520c060.height == 30.3333   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003c79400 UITableViewCellContentView:0x7fae0520c060.height == 500   (active)>

我尝试查看解决方案 here。但它没有帮助(如您所见,我从here 添加了解决方案。

我想要实现的是 100% 自动高度单元格。

谢谢

【问题讨论】:

    标签: swift uitableview uikit tableview uitableviewautomaticdimension


    【解决方案1】:

    不知道为什么会出现这个问题,但是

    tableView.beginUpdates()
    tableView.endUpdates()
    

    以某种方式修复它。

    完整代码

    class ViewController: UITableViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // https://stackoverflow.com/a/27148268/14016301
            
            tableView.estimatedRowHeight = 2.0
            tableView.rowHeight = UITableView.automaticDimension
        }
        
        override func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }
        
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
            
            cell.textLabel?.text = "AAAA"
            let h = cell.contentView.heightAnchor.constraint(equalToConstant: 30)
            h.priority = .defaultHigh
            h.isActive = true
            
            _ = Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { _ in
                h.constant = 500
                self.tableView.beginUpdates()
                self.tableView.endUpdates()
            })
            
            return cell
        }
    
    }
    

    【讨论】:

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