[Switch] translatesAutoresizingMaskIntoConstraints Detailed

In the current knowledge of learning AutoLayout, see some UIView need to set this property, see the article on the Internet to do very detailed notes last night, to be reproduced, invasion delete contact

Original Portal: https://www.jianshu.com/p/d67395deb694

translatesAutoresizingMaskIntoConstraints

  • The autoresizingMask convert Constraints
  • That is: can view frame, bouds, center layout mode automatically converted to the form of constraints. (In this case the constraint on view enough to not need to manually add other constraints)

  • With a view all the code created, translatesAutoresizingMaskIntoConstraints default is YES
  • All created with the view IB, translatesAutoresizingMaskIntoConstraints default NO (AUTORESIZE layout: YES, autolayout layout: NO)

How to set translatesAutoresizingMaskIntoConstraints

  • View uses code to create, frame layout, not to manage translatesAutoresizingMaskIntoConstraints
  • Using code to create the view, autolayout layout, translatesAutoresizingMaskIntoConstraints set to NO
  • View IB creation, frame layout, translatesAutoresizingMaskIntoConstraints not control (IB helped us set up: YES)
  • View IB creation, autolayout layout, translatesAutoresizingMaskIntoConstraints not control (IB to help us set up, NO)

Why translatesAutoresizingMaskIntoConstraints using constraints layout time, we must set to NO?

translatesAutoresizingMaskIntoConstraints intention is to frame layout is automatically converted to the layout constraint, the result of the conversion is automatically added to all the constraints needed for this view, if we then add a constraint to the view that you create will certainly constraint violations.

In order to avoid the above said constraint violation, we create the specified time constraints directly control the layout of this view can not use frame layout (ie translatesAutoresizingMaskIntoConstraints = NO), you can rest assured to use restraint in your code.


example:

v1 is a not use autolayout of view,
v2 is a autolayout of view to use,
but when v1 become a subview v2,
v2 requires four v1 implicit constraint to determine the location of these constraints are converted from the frame v1:

Guess you like

Origin www.cnblogs.com/zhouhui231/p/12118540.html