当我以编程方式更新 Table 视图高度约束时布局被破坏

Layout is Broken when I update the Table View Height Constraint programmatically

当我尝试更新我的 table 视图高度约束常量时,它导致视图中出现错误。 我做错了什么?


  func setupViewHeight() {
    // prepare the animator first and keep a reference to it
    let animator = UIViewPropertyAnimator(duration: 0.5, timingParameters: UICubicTimingParameters(animationCurve: .linear))
    animator.addAnimations {
        self.view.layoutIfNeeded()
    }

    // at some other point in time we change the constraints and call the animator
    self.tableHeightConstraint.constant = CGFloat(self.previousClinics.count) * self.cellHeight

    self.view.setNeedsLayout()
    animator.startAnimation()

    self.tableView.reloadData()
  }

Wrapper View Subview Constraints

Wrapper View Constraints

Buggy View in Action (video)

您可以尝试一件事 -> 将 tableview 高度约束优先级更改为 100 并尝试删除单元格的所有固定高度。 希望对你有帮助。

试试这个 one.It 对我来说效果很好。

let cellHeight = 100.0
var cell : tableCell?
let detailArr : [UIColor] = [.red,.yellow,.black,.blue]

override func viewWillAppear(_ animated: Bool) {
    self.changeTableHeight()
}

func changeTableHeight()
{
    let animator = UIViewPropertyAnimator(duration: 0.5, timingParameters: UICubicTimingParameters(animationCurve: .linear))
    animator.addAnimations {
        self.view.layoutIfNeeded()
    }
    tableviewHeight.constant = CGFloat(Double(detailArr.count) * self.cellHeight)

    self.view.setNeedsLayout()
    animator.startAnimation()

    self.tableview.reloadData()
}