Set UITableView's height to the height of its content with Auto Layout

前端 未结 7 1428
野的像风
野的像风 2020-12-04 13:40

I have a View which has two labels and a Table View inside it. I want label 1 to always stay above my Table View and label 2, to be below the Table View. The problem is that

7条回答
  •  时光说笑
    2020-12-04 14:06

    For TableView Height adjustment automatically with its content size use this in ViewController class.

    //constraintTableViewHeight :- tableview height constraint 
    
    override func viewWillLayoutSubviews() {
        super.updateViewConstraints()
        self.constraintTableViewHeight.constant = self.tableView.contentSize.height
    }
    

    and also add this

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        self.viewWillLayoutSubviews()
    }
    

    This code easily adjust tableview height automatically with its content size.

提交回复
热议问题