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

前端 未结 7 1412
野的像风
野的像风 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 13:45

    In Swift 4.2 and Xcode 10.1

    Here you have two options to set height for UITableView based on content dynamically.

    1) If you get content size use this code in viewDidLayoutSubviews()

    DispatchQueue.main.async {
      var frame = self.TblView.frame
      frame.size.height = self.TblView.contentSize.height
       self.TblView.frame = frame
    }
    

    2) Based on table view height constraint update table view height. After got the response from server when reload table view write this code.

    DispatchQueue.main.async {
        self.TblViewHeightConstraint.constant = CGFloat((self.array.count) * 30)//Here 30 is my cell height
         self.TblView.reloadData()
    }
    
    0 讨论(0)
  • 2020-12-04 13:58

    A more modern way is doing this:

    override func updateViewConstraints() {
        tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true
        super.updateViewConstraints()
    }
    
    0 讨论(0)
  • 2020-12-04 13:59

    You have to override updateViewConstraints() in your UIViewController and set the height constraint's constant to tableView.contentSize.height:

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

    Then you have to make sure that Label2 has a top constraint that is greaterThanOrEqual to the table view's bottom. And you also have to change the table view's height constraint's priority from Required to High to avoid conflicting constraints when the table view's contentHeight is larger than the available height.

    0 讨论(0)
  • 2020-12-04 14:01

    Extending @nova answer to swift 4

    tableViewSizeObservation = tableView.observe(\.contentSize) { [weak self] (tableView, _) in
       tableView.layer.removeAllAnimations()
       self?.tableViewHeightContraint.constant = tableView.contentSize.height
       UIView.animate(withDuration: 0.5) {
             self?.view.updateConstraints()
             self?.view.layoutIfNeeded()
       }
    }
    
    deinit {
        tableViewSizeObservation?.invalidate()
    }
    
    0 讨论(0)
  • 2020-12-04 14:05

    The below code worked for me.

    1. Create an outlet for tableViewHeight.
    @IBOutlet weak var tableViewHeight: NSLayoutConstraint!
    var tableViewHeight: CGFloat = 0  // Dynamically calcualted height of TableView.
    
    1. For the dynamic height of the cell, I used the below code:
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return tableView.estimatedRowHeight
    }
    
    1. For height of the TableView, with dynamic heights of the TableView Cells:
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print(cell.frame.size.height, self.tableViewHeight)
        self.tableViewHeight += cell.frame.size.height
        tableViewBillsHeight.constant = self.tableViewHeight
    }
    

    Explanation:

    After the TableView cell is created, we fetch the frame height of the cell that is about to Display and add the height of the cell to the main TableView.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题