translatesAutoresizingMaskIntoConstraints needs to be set to false when:
- You Create a
UIView-based object in code (Storyboard/NIB will set it for you if the file has autolayout enabled),
- And you want to use auto layout for this view rather than frame-based layout,
- 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
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
false. You will need to ensure that you have sufficient constraints on your subviews for auto layout to determine the size of
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
You also need to return something from
estimatedHeightForHeaderInSection – the closer to your actual header height the better – if you are using
tableview.sectionHeaderHeight = UITableViewAutomaticDimension