Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should translatesAutoresizingMaskIntoConstraints be set to true?

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?

like image 361
mfaani Avatar asked Dec 13 '17 18:12

mfaani


People also ask

What is translate Autoresizing mask?

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.

What is Autoresizingmask in IOS?

An integer bit mask that determines how the receiver resizes itself when its superview's bounds change.

What is Nslayoutconstraint?

The relationship between two user interface objects that must be satisfied by the constraint-based layout system.


1 Answers

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

like image 189
Paulw11 Avatar answered Sep 19 '22 15:09

Paulw11