NSLayoutconstraint and nslayoutconstraint of iOS Chinese API
AutoLayout provides developers with a layout method different from the traditional layout method specified by the UI element location. In the past, both drag and drop in IB and write in code, each UIView has its own frame attribute to define its position and size in the current view. If AutoLayout is used, it is changed to defining the position and size of the view using constraints. The biggest advantage of this is that it solves the adaptation problem of different resolutions and screen sizes, and also simplifies the definition of the position of the view during rotation. The original 10-pixel center view on the bottom, no matter when you rotate the screen or change the device (iPad or iPhone 5 or a mini iPad may appear later), the position remains at the center of 10 pixels above the bottom.
Constraints define the relationship between two users' excuses and meet our requirements by creating constraints (the positions and sizes of controls in the layout system can be seen ). Each constraint is a linear equation in the following format:
item1.attribute1 = multiplier × item2.attribute2 + constant
In this equation, attribute1 and attribute2 are variables that address these constraints when the automatic layout can be adjusted. When you create a constraint to define other values. For example, if you define the relative positions of the two buttons, the start position of the second button is 8 pixels after the end position of the first button. The linear equation is as follows:
button2.leading = 1.0 × button1.trailing + 8.0
In addition. Constraints are not limited to equality. They can also use greater than or equal to (> =) or less than or equal to (<=) to describe the relationship between these two attributes. The constraint also has a priority between 1 and 1000. A constraint with a priority of 1000 is required. All priorities smaller than 1,000 are optional. By default, all constraints are required (priority = 1,000 ). After the constraints are solved, AutoLayout will process all the optional constraints in the highest to lowest priority order. If it cannot solve an optional constraint, it will try to get as close as possible to the desired result and then move to the next constraint. This combination of inequality, equality, and priority gives you great flexibility. By combining multiple constraints, You can dynamically adapt to the size and position of user interface elements in the screen.
Create constraints
+ (NSArray <__kindof NSLayoutConstraint *> *) constraintsWithVisualFormat :( NSString *) format options :( NSLayoutFormatOptions) opts metrics :( nullable NSDictionary <NSString *, id> *) metrics views :( NSDictionary <NSString *, id> *) views
Create Constraints Using VFL description
Parameters:
format |
The format specification of the constraint. |
opts |
Option to describe attributes and layout directions of multiple objects |
metrics |
The constraint that appears in a string in the visual format is dictionary. The key of a dictionary must be an NSString value, and the value of a dictionary must be of the NSNumber type. |
views |
View dictionary. The key of a dictionary must be an NSString value, and the value of a dictionary must be a UIView object. |
Return Value:
Array of constraints
+ (Instancetype) attributes :( id) view1 attribute :( NSLayoutAttribute) attr1 relatedBy :( partial) relation toItem :( partial id) view2 attribute :( NSLayoutAttribute) attr2 multiplier :( CGFloat) multiplier constant :( CGFloat)
Create constraints by defining the relationships between specified attributes of a given View
Parameters:
view1 |
View on the left of the constraint |
attr1 |
Constrain attributes of the left-side view |
relation |
Constrain the relationship between the left and right views |
view2 |
View on the right of the constraint |
attr2 |
Constrain attributes of the view on the right |
multiplier |
Multiplier |
c |
Constant Value |
Activate or deactivate Constraints
Property (getter = isActive) BOOL active
Obtain the status of a constraint. The default value is NO.
+ (Void) activateConstraints :( NSArray <NSLayoutConstraint *> *) constraints
Activate all constraints in the specified constraint array.
+ (Void) deactivateConstraints :( NSArray <NSLayoutConstraint *> *) constraints
Disables all constraints in the specified constraint array.
Access constraint data
@ Property UILayoutPriority priority
Priority attribute. If the priority of a constraint is less than UILayoutPriorityRequired, this constraint is optional. First, the high-priority constraint is satisfied, and then the low-priority constraint is satisfied. The priority must be greater than 0 and less than or equal to NSLayoutPriorityRequired.
@ Property (readonly, assign) id firstItem
The first object involved in the constraint.
@ Property (readonly) NSLayoutAttribute firstAttribute
The attribute of the first object.
@ Property (readonly) NSLayoutRelation relation
Constraint.
@ Property (nullable, readonly, assign) id secondItem
The second object of the parameter constraint.
@ Property (readonly) NSLayoutAttribute secondAttribute;
Attributes of the second object
Enumeration, constant
NSLayoutAttribute
The visual representation of the object, used to obtain a part of the value of the constraint.
Next, I will introduce how to use it. This article ends.