1. The use of the lower right corner of the Stroyboard 4 buttons
From left to right, the 1th button is added to the alignment constraint;
The 2nd button is to add the standard constraint;
The 3rd button allows Xcode to automatically generate constraints or update the view to the correct location;
The 4th button, you can set which classes inherit these constraints, by default, 2 options are selected, it is recommended to leave silbings and ancestors blank, if checked, it becomes difficult to align the views in the view
2. Intrinsic dimensions
When adding constraints, be aware that some controls have intrinsic dimensions, such as segment selectors, sliders, progress bars, progress indicators, and Laibel.
You can see that the height of the slider is grayed out. This is the intrinsic size.
The intrinsic size of the label is special, and the text content determines its width.
3. The meaning of the wireframe representation
3.1 After adding the constraint to the control, the correct situation should be the segment selector (Figure 1).
3.1 If the constraint is ambiguous, it is the label (Figure 2), there is a yellow wireframe and red line segments: The yellow solid wireframe indicates the current design position and size, the top of the label is a red line, the upper margin is ambiguous, the bottom of the red line, there is ambiguity.
3.3 Continue to add a constraint to the label until the ambiguity disappears. will turn into Figure 3, with a yellow solid wireframe and a red dashed box, the meaning of the yellow solid line is still the current design position and size; a red dashed box that indicates the location and size of the label when the program is run.
3.4 With update frame, you can let the label control on the storyboard be updated completely by the automatic layout settings, so that the red dashed box disappears, as shown in Figure 4, figure 4 +28 meaning, indicating that the program allows and the bottom of the progress bar spacing will be more than 28 points, At this point, you can use the update constraints, so that the constraints to meet the current stroyboard display spacing, after the update, the constraints of 40 into 68, see Figure 6
3.5 But generally do not do so, because the constraints are generally not a problem, it should be updated frame, let frame to adapt to the constraints we set, click Fix misplacement, fix dislocation, see Figure 7
Figure 1
Figure 2
Figure 3
Figure 4
Figure 5
Figure 6
Figure 7
4. Top and Bottom layout guide
After IOS7, the controller has toplayoutguide,bottomlayoutguide these 2 properties, in Xcode5 above version, storyboard can see these 2 things.
@interface Uiviewcontroller (Uilayoutsupport)
These objects is used as layout items in the Nslayoutconstraint API
@property (nonatomic,readonly,retain) id<uilayoutsupport> toplayoutguide Ns_available_ios (7_0);
@property (nonatomic,readonly,retain) id<uilayoutsupport> bottomlayoutguide Ns_available_ios (7_0);
@end
When you add a top constraint to a control, you can choose a distance from top layout guide, and the advantage is that top layout guidance contains the height of these xx bars, even if the status bar is present, and if the app is allowed to run in Ios6,ios7 at the same time, Then the benefits are even more pronounced.
5. How to make automatic layout for scrolling view
http://blog.csdn.net/dongtaochen2039/article/details/41749209
6. Automatic layout and Setframe
Controls that use automatic layout do not work even if the control is setframe, because automatic layout has the right to decide
7. Automate layout with code7.1 Adding a constraint to a view
-(void) Viewdidload {
[Super Viewdidload];
UIButton *btn1 = [UIButton buttonwithtype:uibuttontypecontactadd];
[Btn1 Settranslatesautoresizingmaskintoconstraints:no]; //Auto layout and autoresizing are mutually exclusive, so be sure to cancel the autoresizing before using automatic layout
UIButton *btn2 = [UIButton buttonwithtype:uibuttontypecontactadd];
[Btn2 Settranslatesautoresizingmaskintoconstraints:no];
[Self.view ADDSUBVIEW:BTN1]; //Before adding a constraint, be sure to add the handle to the parent view first. Plainly addsubview must be written in front of AddConstraint.
[Self.view ADDSUBVIEW:BTN2]; //If you add a constraint and then add the view to the parent view, the program crashes.
Nslayoutconstraint *layout = [Nslayoutconstraint constraintwithitem:btn1 Attribute:nslayoutattributecenterx Relatedby:nslayoutrelationequal ToItem:self.view attribute:nslayoutattributeleft multiplier:1.0 constant:50]; //representative meaning: btn1 centery = Self.view's top y value * multipler (coefficient) + constant (constant)
Nslayoutconstraint *layout1 = [Nslayoutconstraint constraintwithitem:btn1 attribute: Nslayoutattributecentery relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributetop multiplier:1.0 constant:200];
Nslayoutconstraint *layout2 = [Nslayoutconstraint constraintwithitem:btn2 Attribute:nslayoutattributecenterx Relatedby:nslayoutrelationequal toitem:btn1 Attribute:nslayoutattributecenterx multiplier:1.0 constant:100];
Nslayoutconstraint *LAYOUT3 = [Nslayoutconstraint constraintwithitem:btn2 attribute:nslayoutattributecentery Relatedby:nslayoutrelationequal toitem:btn1 attribute:nslayoutattributetop multiplier:1.0 constant:50];
[Self.view addconstraint:layout];
[Self.view ADDCONSTRAINT:LAYOUT1];
[Self.view Addconstraint:layout2];
[Self.view ADDCONSTRAINT:LAYOUT3];
}
7.2 Adding constraints using the Visual Format language (VFL)
-(void) Viewdidload {
[Super Viewdidload];
UIButton *btn1 = [UIButton buttonwithtype:uibuttontypecontactadd];
[Btn1 Settranslatesautoresizingmaskintoconstraints:no];
[Self.view ADDSUBVIEW:BTN1];
Nsdictionary *viewsdic = nsdictionaryofvariablebindings (BTN1);
Nsarray *constraints1 = [Nslayoutconstraint constraintswithvisualformat:@ "v:|-50-[btn1]" options:0 metrics:nil Views: Viewsdic];
Nsarray *constraints2 = [Nslayoutconstraint constraintswithvisualformat:@ "h:|-100-[btn1]" options:0 metrics:nil views : Viewsdic];
[Self.view Addconstraints:constraints1];
[Self.view addconstraints:constraints2];
}
Whether you're adding a constraint to a view, or using a visual format language, it's too cumbersome, or with a 3rd party framework, such as Uiview+autolayout
IOS Auto Layout