【问题标题】:Getting constraint error while collapsing Tableview section折叠 Tableview 部分时出现约束错误
【发布时间】:2019-01-17 20:29:05
【问题描述】:

我有一个自定义单元格视图,其中包含嵌入在垂直堆栈视图中的图像和标签。

Stack View 绑定到 Content View 的 4 个边缘。

图像的宽高比为 1:1。

展开和折叠操作似乎工作正常,但是当我继续点击时,我在某些时候看到了一些警告,而且似乎是随机的。

2019-01-17 23:15:46.749683+0300 MyApp[10270:349316] [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:0x6000032e54a0 UIStackView:0x7f9f97d19620.height == 43.5   (active)>",
    "<NSLayoutConstraint:0x6000032e5590 V:[UIStackView:0x7f9f97d19620]-(5)-|   (active, names: '|':UITableViewCellContentView:0x7f9f97d19430 )>",
    "<NSLayoutConstraint:0x6000032e5630 V:|-(5)-[UIStackView:0x7f9f97d19620]   (active, names: '|':UITableViewCellContentView:0x7f9f97d19430 )>",
    "<NSLayoutConstraint:0x6000032e5f40 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f9f97d19430.height == 499.5   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000032e54a0 UIStackView:0x7f9f97d19620.height == 43.5   (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.

我运行展开/折叠的内容如下:

....
tableView.register(UINib(nibName: "InboxTableViewCell", bundle: nil), forCellReuseIdentifier: "inboxCell")
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 500
        tableView.reloadData()
....

func numberOfSections(in tableView: UITableView) -> Int {
        return tableViewData.count
    }


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

        if tableViewData[section].opened == true {
            return tableViewData[section].sectionData.count + 1
        }else  {
            return 1
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {

            let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath) as! HeaderTableViewCell
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "inboxCell", for: indexPath) as! InboxTableViewCell

            return cell
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0{
            if tableViewData[indexPath.section].opened == true {
                tableViewData[indexPath.section].opened = false
                let sections = IndexSet.init(integer: indexPath.section)
                tableView.reloadSections(sections, with: .none)

            }else{
                tableViewData[indexPath.section].opened = true
                let sections = IndexSet.init(integer: indexPath.section)
                tableView.reloadSections(sections, with: .none)

            }
        }
    }

【问题讨论】:

  • 你在折叠/展开中运行什么代码?
  • 将堆栈的底部约束降低到 999 并查看控制台中是否仍有该消息
  • stackView 有高度限制吗?您似乎没有,但也许您在代码中设置了这样的约束?
  • 您在stackView的高度上定义了其他约束,可能还有其他约束。而且它们是不相容的。一种方法是为所有约束设置标签(#01、#02、...);您将很容易识别日志中的冲突。
  • @Sh_Khan 降低堆栈的底部约束不起作用

标签: swift uitableview autolayout


【解决方案1】:

将图像的 1:1 纵横比限制的优先级设置为 999 或更低。它强制自动布局为堆栈视图生成高度约束,以保持纵横比。这对企业不利。

【讨论】:

  • 谢谢,但没有用,甚至尝试删除约束
  • 也可以将estimatedRowHeight 设置为自动或0。我也在抓稻草。
  • 你可能很接近原因。我将它设置为 0 并在应用程序的开头显示警报。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2012-07-18
  • 1970-01-01
  • 2011-05-03
相关资源
最近更新 更多