How to make UITableView's height dynamic with autolayout feature?

后端 未结 5 1371
抹茶落季
抹茶落季 2021-01-31 03:32

I am using autolayout in Xcode 5.

I set the table view\'s height to Greater than or equal to 200px. I want that it has dynamic size. Because sometimes it will have many

5条回答
  •  自闭症患者
    2021-01-31 03:44

    You can Ctrl+Drag the height constraint of your UITableView into your view controller source code and give it a name. Then in updateViewConstraints of your ViewController you can set the constant of that constraint to tableView.contentSize.height

    ....

    @IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint?
    

    ....

    override func updateViewConstraints() {
        super.updateViewConstraints()
        tableViewHeightConstraint?.constant = tableView.contentSize.height
    }
    

提交回复
热议问题