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

问题描述 投票:0回答:1

当我尝试更新表格视图高度约束常量时,它会在视图中导致错误。我做错了吗?


  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个回答
0
投票

您可以尝试做一件事->将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()
}

Cell image with constraint

Table view with fix height

Table with orange background color and height as per total number of cell

© www.soinside.com 2019 - 2024. All rights reserved.