NSLayoutConstraint basics for Automatic Layout of IOS pages
Before using AutoLayout, you need to know the following two points: 1. You must set translatesAutoresizingMaskIntoConstraints to NO. 2. For viewControl, AutoLayout adaptation is written in [-updateViewConstraints]; for view adaptation, AutoLayout adaptation is written in [-updateConstraints. I. Methods to be explained:
1 /* Create constraints explicitly. Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant" 2 If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute. 3 */ 4 +(instancetype)constraintWithItem:(id)view1 5 attribute:(NSLayoutAttribute)attr1 6 relatedBy:(NSLayoutRelation)relation 7 toItem:(id)view2 8 attribute:(NSLayoutAttribute)attr2 9 multiplier:(CGFloat)multiplier 10 constant:(CGFloat)c;
Parameter description: The first parameter view1: view to be set; the second parameter attr1: attributes to be set in view1, which will be explained later; the third parameter relation: the relationship between the specified attributes of view1 and view2 will be explained later. The fourth parameter is view2: The reference view. The fifth parameter is attr2: The reference view 2 attribute; the sixth parameter is multiplier. The specified attribute of view view1 is multiple of the attributes defined by reference to view view2. The seventh parameter is c: The floating point number to be added to the specified attribute of view view1. According to the parameter description, the calculation formula is as follows: view1.attr1 [=, >=, <=] view2.attr2 * multiplier + c; Parameter Details: 1. NSLayoutAttribute
1 typedef NS_ENUM(NSInteger, NSLayoutAttribute) { 2 NSLayoutAttributeLeft = 1, 3 NSLayoutAttributeRight, 4 NSLayoutAttributeTop, 5 NSLayoutAttributeBottom, 6 NSLayoutAttributeLeading, 7 NSLayoutAttributeTrailing, 8 NSLayoutAttributeWidth, 9 NSLayoutAttributeHeight,10 NSLayoutAttributeCenterX,11 NSLayoutAttributeCenterY,12 NSLayoutAttributeBaseline,13 NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline,14 NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),15 16 17 NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),18 NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),19 NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),20 NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),21 NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),22 NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),23 NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),24 NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),25 26 NSLayoutAttributeNotAnAttribute = 027 };
Explanation: NSLayoutAttribute Part 1: Common NSLayoutAttributeLeft: CGRectGetMinX (view. frame); NSLayoutAttributeRight: CGRectGetMaxX (view. frame); NSLayoutAttributeTop: CGRectGetMinY (view. frame); NSLayoutAttributeBottom: CGRectGetMinY (view. frame); NSLayoutAttributeWidth: CGRectGetWidth (view. frame); NSLayoutAttributeHeight: CGRectGetHeight (view. frame); NSLayoutAttributeCenterX: view. center. x; NSLayoutAttributeCenterY: view. center. y; NSLayoutAttributeBaseline: the bottom line of the text. In most views, it is equivalent to NSLayoutAttributeBottom. In a few views, such as UILabel, it refers to the position at the bottom of the letter. nslayoutattriattributelastbaseline: over-line text; NSLayoutAttributeNotAnAttribute: None; Part 2: nslayoutbuteleading: in areas that are used to viewing from left to right, nslayoutattriattributeleft; in areas from the right to the left, NSLayoutAttributeRight; NSLayoutAttributeTrailing: in areas from the left to the right, NSLayoutAttributeRight is used. In areas from the right to the left, nslayoutattriattributeleft is used; part 3: new properties and various spacing for ios8. The following section describes nslayoutattrieleftmargin, distance, nslayoutattriattributetopmargin, distance, NSLayoutAttributeLeadingMargin, distance, distance, and distance. A figure is displayed on the Internet, annotation of the above attributes 2, NSLayoutRelation 1 typedef NS_ENUM (NSInteger, NSLayoutRelation) {2 rows =-NSLayoutRelationEqual = rows =}; rows: <=; NSLayoutRelationEqual :=; parameters:> =; 2. method 1 to be explained. Obtain all NSLayoutConstraint 1-(NSArray *) constraints NS_AVAILABLE_IOS (6_0) in the current view. 2. Old method, add or remove the specified NSLayoutConstraint from the page.
1 1 - (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided. Instead, set NSLayoutConstraint's active property to YES.2 2 - (void)addConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided. Instead use +[NSLayoutConstraint activateConstraints:].3 3 - (void)removeConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided. Instead set NSLayoutConstraint's active property to NO.4 4 - (void)removeConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided. Instead use +[NSLayoutConstraint deactivateConstraints:].
3. Add a new method to ios8 to activate or disable the specified constraint.
1 /* The receiver may be activated or deactivated by manipulating this property. Only active constraints affect the calculated layout. Attempting to activate a constraint whose items have no common ancestor will cause an exception to be thrown. Defaults to NO for newly created constraints. */2 @property (getter=isActive) BOOL active NS_AVAILABLE(10_10, 8_0);3 4 /* Convenience method that activates each constraint in the contained array, in the same manner as setting active=YES. This is often more efficient than activating each constraint individually. */5 + (void)activateConstraints:(NSArray *)constraints NS_AVAILABLE(10_10, 8_0);6 7 /* Convenience method that deactivates each constraint in the contained array, in the same manner as setting active=NO. This is often more efficient than deactivating each constraint individually. */8 + (void)deactivateConstraints:(NSArray *)constraints NS_AVAILABLE(10_10, 8_0);
3. Coding Time a> set view1 to a square with width = 20. The first method is width = 20 and Height = 20 1 [self addConstraint: [NSLayoutConstraint constraintWithItem: view1 attribute: inclurelatedby: inclutoitem: nil attribute: inclumultiplier: 1.0 constant: 20]; 2 [self addConstraint: [NSLayoutConstraint constraintWithItem: view1 attribute: inclurelatedby: NSLayoutRelationE Qual toItem: nil attribute: Limit multiplier: 1.0 constant: 20]; second width = 20, Height = width 1 [self addConstraint: [NSLayoutConstraint constraintWithItem: view1 attribute: nslayoutattriattributewidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant: 20]; 2 [self addConstraint: [NSLayoutConstraint constraintWithItem: view1 attribute: NSLa YoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: view1 attribute: NSLayoutAttributeWidth multiplier: 1.0 constant: 0]; the advantage of the second method is that you only need to modify the view1 size. B> set view1.frame. origin. x = view2.frame. origin. x
Required * leftConstraint = [NSLayoutConstraint constraintWithItem: view1 attribute: descrirelatedby: descritoitem: view2 attribute: descrimultiplier: 1.0 constant: 0]; // old method // [self addConstraint: leftConstraint]; // method 1 of the new version [NSLayoutConstraint activateConstraints: @ [leftConstraint]; // method 2leftConstraint of the new version. active = YES;