Imagine a demand situation where a button is displayed on the right and the distance between the top and right of the Superview is 10 and 5, respectively. As shown in the following:
To achieve such a requirement, if not automatic layout technology, then we can think of is honest use of absolute layout of the coordinate calculation to achieve, if the button width is 100, the width of the parent view is 300, then the coordinates of this button is: (300-100-5,10). But if the width of the parent view changes, we have to recalculate it again. Rather troublesome.
Fortunately we have automatic layout technology. It is relatively simple to achieve such a requirement.
Since we want to achieve such a demand, and this demand is actually universal, then we are directly encapsulated well. We add two extended properties to UIView: HorizontalAlignment and VerticalAlignment. All two properties are enumerations.
typedef ns_enum (Nsinteger, uiviewverticalalignment) {Uiviewverticalalignmentfill=0, Uiviewverticalalignmentcenter=1, Uiviewverticalalignmenttop=2, Uiviewverticalalignmentbottom=3};typedef ns_enum (Nsinteger, uiviewhorizontalalignment) {Uiviewhorizontalalignmentfill=0, Uiviewhorizontalalignmentcenter=1, Uiviewhorizontalalignmentleft=2, Uiviewhorizontalalignmentright=3}; @property (nonatomic,assign) uiviewhorizontalalignment HorizontalAlignment; @property (nonatomic,assign) Uiviewverticalalignment VerticalAlignment;
The following ideas are implemented:
I have a horizontal docking below for example, for horizontal docking There are four cases, the first is not to dock the full fill, that is, we put the width of the subview and superview the width of the binding together. The second case is docked on the left, followed by center and right docking.
For non-populated docking, it is certainly not possible to bind directly to the width of the superview in width, only the width of the size of the UIView extended property.
Now take the implementation of the above scenario as an example, the horizontal direction right docking. Then we just have to Subview nslayoutattributeright and Superview nslayoutattributeright alignment. The code is as follows:
[Self.superview addconstraint:[nslayoutconstraint constraintwithitem:self attribute: Nslayoutattributeright relatedby:nslayoutrelationequal ToItem:self.superview Attribute:nslayoutattributeright Multiplier:1.0f Constant:-margin.right]];
For left docking and center docking nothing is the same as aligning the properties. This is also true for vertical docking.
The complete docking code is as follows:
Uiedgeinsets margin=Self.margin; Switch(self.verticalalignment) { CaseUiviewverticalalignmentfill: {nsarray*constraints = [Nslayoutconstraint constraintswithvisualformat:@"v:|-top-[view]-bottom-|"Options0metrics:@{@"Top": @ (Margin.top),@"Bottom": @ (Margin.bottom)} views:@{@"View": self}]; [Self.superview addconstraints:constraints]; } Break; Caseuiviewverticalalignmentbottom: {[Self.superview addconstraint:[nslayoutconstraint constraintWithIt Em:self Attribute:nslayoutattributebottom Relatedby:nslayoutre Lationequal toItem:self.superview attribute:nslayoutattributebottom Multiplier:1.0fconstant:-Margin.bottom]]; } Break; Caseuiviewverticalalignmentcenter: {[Self.superview addconstraint:[nslayoutconstraint constraintWithIt Em:self Attribute:nslayoutattributecentery Relatedby:nslayoutr Elationequal toItem:self.superview attribute:nslayoutattributecentery Multiplier:1.0fconstant0]]; } Break; Caseuiviewverticalalignmenttop: {[Self.superview addconstraint:[nslayoutconstraint Constraintwithitem: Self Attribute:nslayoutattributetop relatedby:nslayoutrelation Equal toItem:self.superview attribute:nslayoutattributetop Multiplier:1.0fConstant:margin.top]]; } Break; default: Break; } Switch(self.horizontalalignment) { Caseuiviewhorizontalalignmentfill:{Nsarray*constraints = [Nslayoutconstraint constraintswithvisualformat:@"|-left-[view]-right-|"Options0metrics:@{@" Left": @ (Margin.left),@" Right": @ (margin.right)} views:@{@"View": self}];//constrain to add width[Self.superview addconstraints:constraints]; } Break; Caseuiviewhorizontalalignmentcenter:{[Self.superview addconstraint:[nslayoutconstraint ConstraintWithItem:sel F Attribute:nslayoutattributecenterx Relatedby:nslayoutrelatio Nequal toItem:self.superview Attribute:nslayoutattributecenterx Multiplier:1.0fconstant0]]; } Break; Caseuiviewhorizontalalignmentleft:{[Self.superview addconstraint:[nslayoutconstraint constraintWithItem:self Attribute:nslayoutattributeleft Relatedby:nslayoutrelationequa L ToItem:self.superview attribute:nslayoutattributeleft Multiplier:1.0fConstant:margin.left]]; } Break; Caseuiviewhorizontalalignmentright:{[Self.superview addconstraint:[nslayoutconstraint constraintWithItem:self Attribute:nslayoutattributeright RELATEDBY:NSLAYOUTRELATIONEQ UAL toItem:self.superview attribute:nslayoutattributeright Multiplier:1.0fconstant:-Margin.right]]; } Break; default: Break; }
For the docking, we wrote in front of Uistackpanel and Uigridview is bound to support. So I changed the original code as well.
Then the code for that button in the diagram is as follows:
UIButton *btn=[[uibutton alloc] Initwithsize:cgsizemake (+)]; [Btn Setbackgroundcolor:[uicolor Bluecolor]; Btn.isbindsizetosuperview=YES; [BTN Settitle: @" button1 " Forstate:uicontrolstatenormal]; Btn.horizontalalignment=uiviewhorizontalalignmentright; Btn.verticalalignment=uiviewverticalalignmenttop; Btn.margin=uiedgeinsetsmake (005); [Self.view addsubview:btn];
Full source code please click to download.
IOS Auto Layout-uistackpanel and Uigridpanel (v)