When should AutoresizingMaskIntoConstraints be set to true?

I read the documentation . But I'm still not sure when I need to not install it in false. In the code below, if I install it in falseI will not see the header at all. If I leave it as truethat, then everything is in order.

The following in the View debug hierarchy will generate a warning that " 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 that whenever I need to change something in the code, I have to set the translatesAutoresizingMaskIntoConstraintsvalue to false.

Perhaps it would be more correct to say whether you need to remove all its restrictions, then set the value for it falseand then add what you like, in which case you will need to add restrictions for all four parties.

However, if you just need to save what the system provides you with, then it will be a tableView that controls its position and width, and then leave the value true.

It is right?

+23
source share
2 answers

translatesAutoresizingMaskIntoConstraints should be set to false when:

  1. You create a UIView-based object in the code (the Storyboard / NIB will set it for you if the automatic placement function is enabled for the file),
  2. , -based,
  3. , .

. , 2.

, viewForHeaderInSection , frame , heightForHeaderInSection.

(header ) .

, autolayout ; , , , .

, header header. header header.

header, translatesAutoresizingMaskIntoConstraints false. , , header.

, , , .

, header translatesAutoresizingMaskIntoConstraints false

- estimatedHeightForHeaderInSection - , - tableview.sectionHeaderHeight = UITableViewAutomaticDimension

+33
  • true, Interface Builder false

    ( ) True, . , . . translatesAutoresizingMaskIntoConstraints NO.

+17

Source: https://habr.com/ru/post/1690728/


All Articles