AutoLayout explanation More is xib and storyboard usage, this article mainly records the pure Code AutoLayout use method:
Method 1. Apple Native method, this method is simple but too complicated, the usability is very poor
//width =superview Height[Superview addconstraint:[nslayoutconstraint constraintwithitem:view1 attribute:nslayoutattributewidth relatedBy: Nslayoutrelationequal Toitem:superview attribute:nslayoutattributewidth Multiplier:1constant0]]; //Height =40[Superview addconstraint:[nslayoutconstraint constraintwithitem:view1 attribute:nslayoutattributeheight relatedBy: Nslayoutrelationequal Toitem:nil attribute:nslayoutattributeheight Multiplier:0constant +]]; //View1 Bottom distance from Superview bottom is 0[Superview addconstraint:[nslayoutconstraint constraintwithitem:view1 attribute:nslayoutattributebottom relatedBy: Nslayoutrelationequal Toitem:superview Attribute:nslayoutattributebottom Multiplier:1constant0]];
Method 2. The VFL method:
// @ " h:|[view1]| options:0 Metrics:nil Views:nsdictionaryofvariablebindings (View1)]; // view1 height is 40, distance from bottom is 0 [ Superview addconstraints:[nslayoutconstraint constraintswithvisualformat:@ " v:|[view1 (==40)] " options:
- V:[view1][view2 (==view1)]|
- V for Vertical layout (vertical), first a view1 with a width equal to view1 width view2,view2 the distance from the bottom edge of the parent view is 0
- H:|-[VIEW1]-[VIEW2]-[VIEW3 (>=20)]-|
- h represents the horizontal layout, view1 the default interval width from the left edge of the parent view, followed by the default width of the view2 distance view1, and then the VIEW3 with a width of not less than 20, which is the default width of View2 and the right edge of the parent view. (Vertical bar ' | ' indicates the edge of Superview, "-|" Represents the default distance, "|" Indicates a distance edge of 0)
Attention:
-(void) AddConstraint: (Nslayoutconstraint *) constraint;
Used to add a constraint to a view. The only thing to note when adding a target view is to follow these rules:
1. Add a constraint to the two same-level view on their parent view
2 for the constraint relationship between two different levels of view, add to their nearest common parent view
3 for a hierarchical relationship between the two view constraints, added to the higher level of the parent view
Method 3. Third-party library masonry
[View1 Settranslatesautoresizingmaskintoconstraints:no];
[View1 mas_makeconstraints:^ (Masconstraintmaker *make) {
Make.leading.width.equalTo (Superview);
Make.height.equalTo (@40);
}];
Refer to the original document:
http://blog.csdn.net/sxfcct/article/details/8776928
http://www.zhihu.com/question/37095424
Masonry Download URL:
Https://github.com/SnapKit/Masonry
Three ways to AutoLayout pure code