AutoLayout for screen adaptation in iOS (beginner)
Source: Internet
Author: User
<span id="Label3"></p><p><p>This is the second blog, come on, hey, get up, today we're going to say when the iOS screen fit, as Apple launched more and more mobile phones, screen size is more and more, and the screen is quite a problem, and today I would like to say, online may have already, I just speak my understanding (maybe not , do not spray .... )</p></p><p><p>AutoLayout is actually constrained, today is the code to add constraints, the use of the third party is masonry, I believe that the code to write constraints know this third-party library, well, nonsense not much to say, the code to start</p></p><p><p>First of all you have to download a masonry, or add to the project with cocoapods, first A simple example ah, the <span style="color: #ff0000;">following please focus on the comments Ah</span></p></p><pre><pre>UIView *view1 =<span style="color: #000000;"> [[UIView alloc] init]; </span> =<span style="color: #000000;"> [uicolor blackcolor]; [self.view addsubview:view1]; [view1 mas_makeconstraints:</span>^ (masconstraintmaker * make<span style="color: #000000;">) { </span><span style="color: #008000;">//</span> <span style="color: #008000;"> The midpoint coordinate of the view1 is equal to the midpoint of the Self.view</span> <span style="color: #000000;"> make.center.equalTo (self.view); </span> <span style="color: #008000;">//</span> <span style="color: #008000;">set the size of View1 to width 300, height 300, where mas_equalto seems to be set for specific values, and Equalto is not</span> Make.size.mas_equalTo (cgsizemake)<span style="color: #800080;"></span><span style="color: #800080;"></span><span style="color: #000000;">; }];</span></pre></pre><p><p>Run effect</p></p><p><p><br></p></p><p><p>You can see the screen is the same, in fact, AutoLayout learning good words, only need to write a set of code to fit so size, and not afraid of Apple later in the other dimensions, once and for all (is the code particularly Annoying)</p></p><p><p>now, a little complicated, 2 views, The first view on the left distance from the parent view 20, The second view to the right distance from the parent view 30, 2 block View equal width interval 10</p></p><pre>UIView *view1 =<span style="color: #000000;"><span style="color: #000000;">[[UIView alloc] init]; View1.backgroundcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor blackcolor]; [self.view addsubview:view1]; UIView</span></span>*view2 =<span style="color: #000000;"><span style="color: #000000;">[[UIView alloc] init]; View2.backgroundcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor cyancolor]; [self.view addsubview:view2]; [view1 mas_makeconstraints:</span></span>^ (masconstraintmaker *<span style="color: #000000;">make <span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">The left side of the View1 is the left distance from <span style="color: #008000;">Self.view</span></span>Make.left.equalTo (self.view.mas_left). Offset (<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">View1 Right distance view2 left is 10, why is negative, actually to the right is positive, left negative (i understand ah), top and Buttom same</span></span>Make.right.equalTo (view2.mas_left). Offset (-<span style="color: #800080;"><span style="color: #800080;">Ten</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">meaning of this sentence is that the Y value of the midpoint of the view1 equals the Y value of the midpoint of Self.view</span> .</span><span style="color: #000000;"><span style="color: #000000;">Make.centerY.mas_equalTo (self.view.mas_centerY); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the height of the view1 is 150, the object is used Here.</span></span>Make.height.mas_equalTo (@<span style="color: #800080;"><span style="color: #800080;"></span> max</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the width of the view1 equals View2</span></span><span style="color: #000000;"><span style="color: #000000;">Make.width.equalTo (view2); }]; [view2 mas_makeconstraints:</span></span>^ (masconstraintmaker *<span style="color: #000000;">make <span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">The left side of the VIEW2 is 10 from the right side of <span style="color: #008000;">view1, which is actually 10.</span></span>Make.left.equalTo (view1.mas_right). Offset (<span style="color: #800080;"><span style="color: #800080;">Ten</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">View2 's right distance to the right of Self.view is-30, Why the brain is negative</span> .</span>Make.right.equalTo (self.view.mas_right). Offset (-<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">meaning of this sentence is that the Y value of the midpoint of the view2 equals the Y value of the midpoint of Self.view</span> .</span><span style="color: #000000;"><span style="color: #000000;">Make.centerY.mas_equalTo (self.view.mas_centerY); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the height of the view2 is 150, the object is used Here.</span></span>Make.height.mas_equalTo (@<span style="color: #800080;"><span style="color: #800080;"></span> max</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the width of the view2 equals View1</span></span><span style="color: #000000;"><span style="color: #000000;">Make.width.equalTo (view1); }];</span></span></pre><p><p>Run the following effect</p></p><p><p></p></p><p><p>is not very good, we will write a 3 view, of which 2 view is the same position as above, the third view on the right side of the second view, the distance is 15, the third view of the right distance from the parent view distance of 20, I hope that the small partners themselves to try, If you can knock it out again, it proves that masonry is getting started.</p></p><pre>UIView *view1 =<span style="color: #000000;"><span style="color: #000000;">[[UIView alloc] init]; View1.backgroundcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor blackcolor]; [self.view addsubview:view1]; UIView</span></span>*view2 =<span style="color: #000000;"><span style="color: #000000;">[[UIView alloc] init]; View2.backgroundcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor cyancolor]; [self.view addsubview:view2]; UIView</span></span>*VIEW3 =<span style="color: #000000;"><span style="color: #000000;">[[UIView alloc] init]; View3.backgroundcolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">[uicolor redcolor]; [self.view addsubview:view3]; [view1 mas_makeconstraints:</span></span>^ (masconstraintmaker *<span style="color: #000000;">make <span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">The left side of the View1 is the left distance from <span style="color: #008000;">Self.view</span></span>Make.left.equalTo (self.view.mas_left). Offset (<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">View1 Right distance view2 left is 10, why is negative, actually to the right is positive, left negative (i understand ah), top and Buttom same</span></span>Make.right.equalTo (view2.mas_left). Offset (-<span style="color: #800080;"><span style="color: #800080;">Ten</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">meaning of this sentence is that the Y value of the midpoint of the view1 equals the Y value of the midpoint of Self.view</span> .</span><span style="color: #000000;"><span style="color: #000000;">Make.centerY.mas_equalTo (self.view.mas_centerY); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the height of the view1 is 150, the object is used Here.</span></span>Make.height.mas_equalTo (@<span style="color: #800080;"><span style="color: #800080;"></span> max</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the width of the view1 equals View2</span></span><span style="color: #000000;"><span style="color: #000000;">Make.width.equalTo (view2); }]; [view2 mas_makeconstraints:</span></span>^ (masconstraintmaker *<span style="color: #000000;">make <span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">The left side of the view2 is the <span style="color: #008000;">right distance from View1</span></span>Make.left.equalTo (view1.mas_right). Offset (<span style="color: #800080;"><span style="color: #800080;">Ten</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">View2 's right distance to the right of view3 is-15, Why the brain is negative</span> .</span>Make.right.equalTo (view3.mas_left). Offset (-<span style="color: #800080;"><span style="color: #800080;"></span> the</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">meaning of this sentence is that the Y value of the midpoint of the view2 equals the Y value of the midpoint of Self.view</span> .</span><span style="color: #000000;"><span style="color: #000000;">Make.centerY.mas_equalTo (self.view.mas_centerY); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the height of the view2 is 150, the object is used Here.</span></span>Make.height.mas_equalTo (@<span style="color: #800080;"><span style="color: #800080;"></span> max</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the width of the view2 equals VIEW3</span></span><span style="color: #000000;"><span style="color: #000000;">Make.width.equalTo (view3); }]; [VIEW3 mas_makeconstraints:</span></span>^ (masconstraintmaker *<span style="color: #000000;">make <span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">The left side of the VIEW3 is the <span style="color: #008000;">right distance from View2</span></span>Make.left.equalTo (view2.mas_right). Offset (<span style="color: #800080;"><span style="color: #800080;"></span> the</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">View3 's right distance to the right of Self.view is-20, Why the brain is negative</span> .</span>Make.right.equalTo (self.view.mas_right). Offset (-<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">meaning of this sentence is that the Y value of the midpoint of the view3 equals the Y value of the midpoint of Self.view</span> .</span><span style="color: #000000;"><span style="color: #000000;">Make.centerY.mas_equalTo (self.view.mas_centerY); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the height of the VIEW3 is 150, the object is used Here.</span></span>Make.height.mas_equalTo (@<span style="color: #800080;"><span style="color: #800080;"></span> max</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the width of the view3 equals View1</span></span><span style="color: #000000;"><span style="color: #000000;">Make.width.equalTo (view1); }];</span></span></pre><p><p>Run effect</p></p><p><p></p></p><p><p>Today's Blog is written here, I hope you can get started, but not in the project with this constraint, are proportional write adaptation (in fact, The effect is not good, but not autolayout so troublesome), in the future of the project to try masonry write adaptation, in fact, these are my own understanding, If not, please correct me, thank you.</p></p><p><p>AutoLayout for screen adaptation in iOS (beginner)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service