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?
By default, the system translates the view's autoresizing mask into a set of constraints fully specifying the view's size and position. This also means by default you cannot add constraints to modify the view's frame without introducing conflicts. The view will be displayed at position (0,0) with width and height of 0.
An integer bit mask that determines how the receiver resizes itself when its superview's bounds change.
The relationship between two user interface objects that must be satisfied by the constraint-based layout system.
translatesAutoresizingMaskIntoConstraints
needs to be set to false when:
UIView
-based object in code (Storyboard/NIB will set it for you if the file has autolayout enabled), 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With