Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

ios - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...