iOS: Dynamically set UITableView height in Swift

前端 未结 2 1789
孤街浪徒
孤街浪徒 2020-12-29 08:27

Our UITableView exists on a ViewController (and not a TableViewController). We populate it at run time with two rows. When we set a height constraint the table appears fin

2条回答
  •  梦毁少年i
    2020-12-29 09:29

    You can change tableView frame in multiple ways, with animations etc.

    tableView.frame = CGRect(x: leftTableBorder, y: topTableBorder, width: tableWidth, height: tableHeight)
    

    Or you can change tableView related constraints and then update constraints with layoutIfNeeded, also animated if you wish.

    For example if you have a height constraint property:

    tableViewHeightConstraint.constant = desiredHeight
    
    layoutIfNeeded()
    

    In any case, constraints are needed, so autolayout knows how to render the table.

提交回复
热议问题