When should translatesAutoresizingMaskIntoConstraints be set to true?

Honey picture Honey · Dec 13, 2017 · Viewed 26.1k times · Source

I've read the documentation. But I'm still not sure when I need to not set it to false. In the code below if I set it to false I won't see the header at all. If I leave it as true, then everything is fine.

The following in View debug hierarchy will give a warning "width and position are ambiguous".

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let header = UIView()
    header.translatesAutoresizingMaskIntoConstraints = false
    header.backgroundColor = .orange
    header.heightAnchor.constraint(equalToConstant: 10).isActive = true

    return header
}

I thought whenever I need to modify anything in the code I would have to set translatesAutoresizingMaskIntoConstraints to false.

Perhaps it's more correct to say if you need to remove all its constraints then set it to false and then add what you like, and in that case you would need to add constraints for all 4 sides.

However, if you need to just keep what the system provides to you, in this case that would be the tableView managing its position and width then leave to true.

Is that right?

Answer

Paulw11 picture Paulw11 · Dec 13, 2017

translatesAutoresizingMaskIntoConstraints needs to be set to false when:

  1. You Create a UIView-based object in code (Storyboard/NIB will set it for you if the file has autolayout enabled),
  2. And you want to use auto layout for this view rather than frame-based layout,
  3. And the view will be added to a view hierarchy that is using auto layout.

In this case not all of these are true. Specifically, point 2.

After you return the header view from viewForHeaderInSection it is added to the table view and its frame is set based on the current width of the table view and the height you return from heightForHeaderInSection.

You can add subviews to the root header view (header in your code) and use constraints to layout those subviews relative to the header view.

You have discovered the reason why you can't use autolayout for the header view itself in your comments; at the time you create the view it isn't yet part of the view hierarchy and so you cannot constrain its edges to anything.

In order to have dynamic header sizing, you will need to add subviews to your header view and add constraints between those subviews and header. Then, auto layout can use the intrinsic content size of header to determine the header view size.

Since you are not constraining the frame of header, do not set translatesAutoresizingMaskIntoConstraints to false. You will need to ensure that you have sufficient constraints on your subviews for auto layout to determine the size of header.

You will need a continuous line of constraints from top to bottom and potentially some height constraints for your subviews if the intrinsic content size of that subview is not sufficient.

Any subviews you add to header do need translatesAutoresizingMaskIntoConstraints set to false

You also need to return something from estimatedHeightForHeaderInSection - the closer to your actual header height the better - if you are using tableview.sectionHeaderHeight = UITableViewAutomaticDimension