The end result is as follows:
Many of the constraints have been applied to the view, and we are explaining:
• There are two gray views in the main view of our views controller. Two views from view Controller view left
There is a standard blank distance between the edge and the right. The top of the view at the top of the view must have a standard blank
Distance. There is a standard vertical white space distance between the two gray views.
• There is a button in the vertical center of the two Gray view.
• The button in the gray view above has a standard white space distance from the left side of its parent view.
• The left edge of the button in the gray view below should be aligned to the left border of the button in the gray view above.
This is the cross view constraint, which is important to us.
• The gray view should be resized according to the direction of the view controller. • The height of the two gray views must be 100 pixels.
thirdviewcontroller.m//autolayoutdemo////Created by Wildcat on 14-4-22.//Copyright (c) 2014 Com.wildcat. All rights reserved.//#import "ThirdViewController.h" @interface Thirdviewcontroller () @property (nonatomic, Strong) UIView *topgrayview; @property (nonatomic, strong) UIButton *topbutton; @property (nonatomic, strong) UIView * Bottomgrayview; @property (nonatomic, strong) UIButton *bottombutton; @end @implementation thirdviewcontroller-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [Super Initwithnibname: Nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} -(void) viewdidload{[Super Viewdidload]; [Self creategrayviews]; [Self createbuttons]; [Self applyconstraintstotopgrayview]; [Self applyconstraintstobuttonontopgrayview]; [Self applyconstraintstobottomgrayview]; [Self applyconstraintstobuttononbottomgrayview];} -(void) didreceivememorywarning{[Super DidreCeivememorywarning]; Dispose of any resources the can be recreated.} #pragma mark-#pragma mark definition creates a gray view function-(UIView *) newgrayview{UIView *result = [[UIView alloc] init]; Result.backgroundcolor = [Uicolor Lightgraycolor]; Result.translatesautoresizingmaskintoconstraints = NO; [Self.view Addsubview:result]; return result;} View initialization-(void) creategrayviews{Self.topgrayview = [self newgrayview]; Self.bottomgrayview = [self Newgrayview];} #pragma mark Create button-(UIButton *) Newbuttonplacedonview: (UIView *) paramview{UIButton *result = [UIButton buttonwithtype: Uibuttontyperoundedrect]; Result.translatesautoresizingmaskintoconstraints = NO; [Result settitle:@ "button" forstate:uicontrolstatenormal]; [Paramview Addsubview:result]; return result;} Add to view-(void) createbuttons{Self.topbutton = [self newButtonPlacedOnView:self.topGrayView]; Self.bottombutton = [self NewButtonPlacedOnView:self.bottomGrayView];} #pragma mark-add constraint #pragma mark adds a constraint to the view above- (void) applyconstraintstotopgrayview{nsdictionary *views = nsdictionaryofvariablebindings (_topGrayView); Nsmutablearray *constraints = [[Nsmutablearray alloc] init]; NSString *const khconstraint = @ "h:|-[_topgrayview]-|"; /define Horizontal constraint nsstring *const kvconstraint = @ "V:|-[_topgrayview (==100)]";//define VERTICAL constraint/* horizontal constraint (s) */[cons Traints Addobjectsfromarray: [Nslayoutconstraint constraintswithvisualformat:khconstraint options:0 Metrics:nil Views:views]]; /* Vertical constraint (s) */[Constraints Addobjectsfromarray: [Nslayoutconstraint Constraintswithvisualformat:kvco Nstraint options:0 Metrics:nil Views:views]]; [Self.topGrayView.superview addconstraints:constraints];} Add a constraint to the top button-(void) applyconstraintstobuttonontopgrayview{Nsdictionary *views = nsdictionaryofvariablebindings (_topButton); Nsmutablearray *constraints = [[Nsmutablearray alloc] init]; NSString *const khconstraint = @ "H:|-[_topbutton]"; /* Horizontal constraint (s) */[Constraints Addobjectsfromarray: [Nslayoutconstraint Constraintswithvisualformat:kh Constraint options:0 Metrics:nil Views:views]]; /* Vertical constraint (s) */[Constraints AddObject: [Nslayoutconstraint ConstraintWithItem:self.topButton Attribute:nslayoutattributecentery relatedby:nslayoutrelationequ Al ToItem:self.topGrayView Attribute:nslayoutattribut Ecentery multiplier:1.0f constant:0.0f]]; [Self.topButton.supErview addconstraints:constraints];} #pragma mark adds a constraint to the bottom view-(void) applyconstraintstobottomgrayview{nsdictionary *views = nsdictionaryofvariablebindings (_topgrayview, _bottomgrayview); Nsmutablearray *constraints = [[Nsmutablearray alloc] init]; NSString *const khconstraint = @ "h:|-[_bottomgrayview]-|"; NSString *const kvconstraint = @ "V:|-[_topgrayview]-[_bottomgrayview (==100)]"; /* Horizontal constraint (s) */[Constraints Addobjectsfromarray: [Nslayoutconstraint Constraintswithvisualformat:kh Constraint options:0 Metrics:nil Views:views]]; /* Vertical constraint (s) */[Constraints Addobjectsfromarray: [Nslayoutconstraint Constraintswithvisualformat:kvco Nstraint options:0 Metrics:nil V Iews:views]]; [Self.bottomGrayView.superview addconstraints:constraints];} to the bottomThe button add constraint-(void) applyconstraintstobuttononbottomgrayview{nsdictionary *views = Nsdictionaryofvariablebindings (_ Topbutton, _bottombutton); NSString *const khconstraint = @ "H:[_topbutton][_bottombutton]"; /* Horizontal constraint (s) */[Self.bottomGrayView.superview addconstraints: [Nslayoutconstraint Constraintswithvisualformat:khconstraint options:0 Metrics:nil Views:views]]; /* Vertical constraint (s) */[Self.bottomButton.superview addconstraint: [Nslayoutconstraint Constraintwithitem:sel F.bottombutton attribute:nslayoutattributecentery relatedby:nslayoutrelationequal toIt Em:self.bottomGrayView Attribute:nslayoutattributecentery multiplier:1.0f constant:0.0f]];} -(Nsuinteger) supportedinterfaceorientations{return Uiinterfaceorientationmaskall;} @end
not to be continued. Translated from: http://blog.csdn.net/wildcatlele/article/details/24961323