【问题标题】:Layout is Broken when I update the Table View Height Constraint programmatically当我以编程方式更新表视图高度约束时,布局被破坏
【发布时间】:2020-07-20 19:27:45
【问题描述】:

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


  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)

【问题讨论】:

    标签: ios swift xcode autolayout storyboard


    【解决方案1】:

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

    试试这个。它非常适合我。

    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()
    }
    

    【讨论】:

    • 没用,很遗憾,还有其他建议吗?我几乎尝试了所有方法!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2014-01-22
    相关资源
    最近更新 更多