AutoLayout provides developers with a different layout method than traditional for UI element placement. Previously, whether you were dragging or dropping in IB or writing in code, each uiview had its own frame property to define its position and size in the current view. With AutoLayout, it becomes the use of constraints to define the location and dimensions of the view. The biggest advantage of this is that it solves the problem of view adaptation in different resolutions and screen sizes, and simplifies the definition of the position of the view at the point of rotation, with a view that is centered 10 pixels above the bottom. Whether you're rotating the screen or changing the device (ipad or IPhone5, or the mini ipad that might appear later), it's always centered 10 pixels above the bottom and doesn't change.
Constraints define the relationship between two user excuses by creating constraints that meet our requirements (the position and size of the visible control in the layout system). Each constraint is a linear equation that has the following format:
item1.attribute1 = multiplier × item2.attribute2 + constant
In this equation, attribute1 and attribute2 are variables that resolve these constraints when automatic layout can be adjusted. Other values are defined when you create the constraint. For example, if you define a relative position of two buttons, the starting position of the second button is 8 pixels from the end position of the first button. This relationship is a linear equation as follows:
button2.leading = 1.0 × button1.trailing + 8.0
Other than that. Constraints are not confined to equal relations. They can also describe the relationship between these two properties using greater than or equal to (> =) or less than or equal to (< =). Constraints also have a priority between 1 and 1000. A constraint with a priority of 1000 is required. All priority levels that are less than 1,000 are optional. By default, all constraints are required (priority = 1,000). After resolving the desired constraint, AutoLayout will handle all optional constraints from the highest to lowest order of precedence, and if it does not resolve an optional constraint, it will try to be as close as possible to the desired result and then move to the next constraint. This combination of inequality, equality, and prioritization gives you a great degree of flexibility. By combining multiple constraints, you can define the size and position of a user interface element dynamically adapted to the screen.
Create a constraint
+ (nsarray<__kindof nslayoutconstraint *> *) Constraintswithvisualformat: (NSString *) format options: ( Nslayoutformatoptions) OPTs metrics: (Nullable nsdictionary<nsstring *,id> *) metrics views: (nsdictionary< NSString *, id> *) views
Creating constraints with the VFL description
Parameters:
format |
The format specification for the constraint. |
opts |
Options describe the properties of multiple objects and the orientation of the layout |
metrics |
The constraint that appears in the visual format string Dictionary,dictionary key must be a nsstring value, and the value of dictionary must be of type NSNumber. |
views |
The key of the view dictionary,dictionary must be a nsstring value, and the value of dictionary must be a UIView object. |
return value:
Array of Constraints
+ (Instancetype) Constraintwithitem: (id) view1 attribute: (nslayoutattribute) attr1 Relatedby: (nslayoutrelation) Relation Toitem: (nullable ID) view2 attribute: (nslayoutattribute) attr2 multiplier: (cgfloat) Multiplier constant: ( CGFloat) C
Creates a constraint by defining a relationship between the specified properties of a given view
Parameters:
view1 |
View to the left of the constraint |
attr1 |
Constrain properties of the left view |
relation |
Constrain the relationship between the left and right views |
view2 |
View to the right of the constraint |
attr2 |
Constrain the properties of the right view |
multiplier |
Multiplier |
c |
Constant value |
Activate or deactivate a constraint
Property (getter=isactive) BOOL active
Gets the state of a constraint. The default is No.
+ (void) activateconstraints: (nsarray<nslayoutconstraint *> *) constraints
Activates all constraints in the specified constraint array.
+ (void) deactivateconstraints: (nsarray<nslayoutconstraint *> *) constraints
Disables all constraints in the specified constraint array.
accessing constraint data
@property uilayoutpriority Priority
The priority attribute. If the precedence of a constraint is less than uilayoutpriorityrequired, then this constraint is optional. The constraint that satisfies the high priority first can satisfy the low priority. The priority must be greater than 0 and less than or equal to nslayoutpriorityrequired.
@property (readonly, assign) ID FirstItem
The first object that participates in a constraint.
@property (readonly) Nslayoutattribute FirstAttribute
The property of the first object.
@property (readonly) nslayoutrelation relation
Constraint, the relationship of 2 attributes.
@property (Nullable, ReadOnly, assign) ID SecondItem
The second object of the parameter constraint.
@property (readonly) Nslayoutattribute Secondattribute;
Properties of the second object
Enumeration, constants
Nslayoutattribute
A visual representation of the object that is used to get part of the value of the constraint.
The following article describes how to use this article to end
iOS Chinese API Nslayoutconstraint