Dynamic height for static table cells with wrapping labels?

Viewed 8356

My text is two lines long in portrait mode. When I switch to landscape mode, it fits into a single line. I'm using static tableview cells via a storyboard; how can I resize the row to fit snugly?

The screen is a signin screen.

  • The first cell contains some explanation text
  • The second is a text field for entering the account name
  • The third is a secure text field for entering the password
  • The fourth (and last) cell contains the signin button. The return key on the keyboard submits the form or switches the focus as appropriate
3 Answers

I have gotten this to work using proper constraints where the label has top and bottom constraints set and returning the UITableViewAutomaticDimension in the heightForRowAt like this

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

In my case I had several labels inside a stack view and I had to set the stack view top and bottom to the contentView in order for the cell to grow.

Related