Nslayoutconstraint basics of automatic layout of iOS pages

Source: Internet
Author: User
<span id="Label3"></p><p><p>You need to know the following two points before using autolayout:</p></p><p><p>1. Translatesautoresizingmaskintoconstraints must be set to No.</p></p><p><p>2. If the Viewcontrol is autolayout, it is written in [-updateviewconstraints];</p></p><p><p><span style="line-height: 1.5;">If it is a view then the AutoLayout Fit is written in [-updateconstraints].</span></p></p><p><p></p></p><p><p>first, <span style="line-height: 1.5;">to explain the method:</span></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">Create Constraints Explicitly. Constraints is of the form "VIEW1.ATTR1 = view2.attr2 * multiplier + constant"</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008000;"><span style="color: #008000;">If your equation does not has a second view and attribute, use nil and Nslayoutattributenotanattribute.</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>+ (instancetype) Constraintwithitem: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) View1</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;">attribute: (nslayoutattribute) attr1</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #000000;"><span style="color: #000000;">relatedby: (nslayoutrelation) Relation</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span>Toitem: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) View2</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;">attribute: (nslayoutattribute) attr2</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #000000;"><span style="color: #000000;">multiplier: (cgfloat) Multiplier</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>Constant: (cgfloat) c;</pre><p><p></p></p><p><p>Parameter description:</p></p><p><p>First parameter view1: the view to be set;</p></p><p><p>The second parameter attr1:view1 the property to be set, which is explained later;</p></p><p><p>The third parameter relation: the relationship between the specified properties of the view View1 and view2, as explained later;</p></p><p><p>The fourth parameter view2: the referenced view;</p></p><p><p>The fifth parameter attr2: refer to the properties of the view view2, explained later;</p></p><p><p>The sixth parameter multiplier: the specified property of the view View1 is the number of times the property is view2 by the reference view;</p></p><p><p>The seventh parameter C: The specified property of the view View1 the floating-point number to add.</p></p><p><p></p></p><p><p>According to the explanation of the parameters, the calculation formula is as Follows:</p></p><p><p>VIEW1.ATTR1 [=, >=, <=] view2.attr2 * multiplier + c;</p></p><p><p></p></p><p><p>Detailed Parameters:</p></p><p><p>1, Nslayoutattribute</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #000000;"><span style="color: #000000;">typedef ns_enum (nsinteger, Nslayoutattribute) {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Nslayoutattributeleft =<span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">, </span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributeright,</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributetop,</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributebottom,</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributeleading,</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributetrailing,</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributewidth,</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributeheight,</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributecenterx,</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributecentery,</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributebaseline,</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Nslayoutattributelastbaseline =<span style="color: #000000;"><span style="color: #000000;">nslayoutattributebaseline,</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributefirstbaseline Ns_enum_available_ios (8_0),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributeleftmargin Ns_enum_available_ios (8_0),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributerightmargin Ns_enum_available_ios (8_0),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributetopmargin Ns_enum_available_ios (8_0),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributebottommargin Ns_enum_available_ios (8_0),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributeleadingmargin Ns_enum_available_ios (8_0),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributetrailingmargin Ns_enum_available_ios (8_0),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributecenterxwithinmargins Ns_enum_available_ios (8_0),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">nslayoutattributecenterywithinmargins Ns_enum_available_ios (8_0),</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span>Nslayoutattributenotanattribute =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>};</pre><p><p>Explain Nslayoutattribute in three parts<br><br></p></p><p><p>The first part: common</p></p><p><p><span class="s1">nslayoutattributeleft:</span> <span class="s1">Cgrectgetminx (view.frame)</span> <span style="line-height: 1.5;">;</span></p></p><p><p><span class="s1"><span class="s1">Nslayoutattributeright: <span class="s1">Cgrectgetmaxx (view.frame);</span></span></span></p></p><p><p><span class="s1"><span class="s1"><span class="s1">Nslayoutattributetop: <span class="s1">cgrectgetminy (view.frame);</span></span></span></span></p></p><p><p><span class="s1"><span class="s1"><span class="s1"><span class="s1">nslayoutattributebottom: <span class="s1">cgrectgetminy (view.frame);</span></span></span></span></span></p></p><p><p><span class="s1">Nslayoutattributewidth:</span> <span class="s1">cgrectgetwidth (view.frame)</span> <span class="s1"><span class="s1">;</span></span></p></p><p><p>Nslayoutattributeheight:cgrectgetheight (view.frame)<span class="s1">;</span></p></p><p><p><span class="s1">Nslayoutattribute</span> <span class="s1">CenterX</span> <span class="s1">: view.center.x<span class="s1">;</span> </span></p></p><p><p><span class="s1"><span class="s1"><span class="s1">Nslayoutattribute<span class="s1">centery<span class="s1">: view.center.y <span class="s1">;</span> </span> </span></span></span></span></p></p><p><p>nslayoutattributebaseline: text Bottom line, which is equivalent to Nslayoutattributebottom in most views; in a few views, such as uilabel, refers to the position at the bottom of the letter <span class="s1">;</span></p></p><p><p><span class="s1">Nslayoutattributelastbaseline: equivalent to Nslayoutattributebaseline<span class="s1">;</span></span></p></p><p><p><span class="s1"><span class="s1">Nslayoutattributefirstbaseline: text on the marking <span class="s1">;</span></span></span></p></p><p><p><span class="s1"><span class="s1">Nslayoutattribute<span class="s1">notanattribute:none<span class="s1">;</span> </span></span></span></p></p><p><p></p></p><p><p><span class="s1"><span class="s1">Part two: different meanings according to the National usage habits</span></span></p></p><p><p><span class="s1"><span class="s1"><span class="s1"><span class="s1"><span class="s1">Nslayoutattributeleading: in the customary left-to-right area, the equivalent of nslayoutattributeleft, in the habit from right to left to see the area, equivalent to Nslayoutattributeright<span class="s1">;</span></span></span></span></span></span></p></p><p><p>Nslayoutattribute<span class="s1">Trailing: in the customary left-to-right area, the equivalent of nslayoutattributeright, in the habit from right to left to see the area, equivalent to Nslayoutattributeleft<span class="s1">;</span> </span></p></p><p><p></p></p><p><p><span class="s1">Part Iii: iOS8 new properties, various spacing, specific usage the following section describes</span></p></p><p><p><span class="s1">nslayoutattributeleftmargin,<span class="s1"><br></span></span></p></p><p><p>nslayoutattributerightmargin,<span class="s1"><br></span></p></p><p><p><span class="s1">nslayoutattributetopmargin,<span class="s1"><br></span></span></p></p><p><p>nslayoutattributebottommargin,<span class="s1"><br></span></p></p><p><p><span class="s1">nslayoutattributeleadingmargin,<span class="s1"><br></span></span></p></p><p><p><span class="s1"><span class="s1">nslayoutattributetrailingmargin,<span class="s1"><br></span></span></span></p></p><p><p>nslayoutattributecenterxwithinmargins,<span class="s1"><br></span></p></p><p><p><span class="s1">nslayoutattributecenterywithinmargins,<span class="s1"><br></span></span></p></p><p><p></p></p><p><p>From the Internet to find a picture, mark the above attributes</p></p><p><p></p></p><p><p>2,<span class="s1">nslayoutrelation</span></p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #000000;">typedef ns_enum (nsinteger, Nslayoutrelation) {</span><span style="color: #008080;">2</span> Nslayoutrelationlessthanorequal =-<span style="color: #800080;">1</span><span style="color: #000000;">,</span><span style="color: #008080;">3</span> <span style="color: #800080;">0</span><span style="color: #000000;">,</span><span style="color: #008080;">4</span> <span style="color: #800080;">1</span> <span style="color: #000000;">,</span> <span style="color: #008080;">5</span> };</pre></pre><p><p><span class="s1">nslayoutrelationlessthanorequal: <=;</span></p></p><p class="p1"><p class="p1"><span class="s1">Nslayoutrelationequal: =;</span></p></p><p class="p1"><p class="p1"><span class="s1">Nslayoutrelationgreaterthanorequal: >=;</span></p></p><p class="p1"><p class="p1"></p></p><p class="p1"><p class="p1"><span class="s1">second, the method to be explained</span></p></p><p class="p1"><p class="p1"><span class="s1">1. Get all of the current view</span> Nslayoutconstraint</p></p><pre><pre><span style="color: #008080;">1</span> -(nsarray *) constraints Ns_available_ios (6_0);</pre></pre><p><p>2, the old method, add the specified nslayoutconstraint to the page or remove from the page</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #800080;"><span style="color: #800080;">1</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) addconstraint: (nslayoutconstraint *) constraint Ns_available_ios (6_0);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">this <span style="color: #008000;">method is deprecated in a future release and should is Avoided. Instead, set Nslayoutconstraint ' s active property to YES.</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #800080;"><span style="color: #800080;">2</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) addconstraints: (nsarray *) constraints Ns_available_ios (6_0);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">this <span style="color: #008000;">method is deprecated in a future release and should is Avoided. Instead Use +[nslayoutconstraint activateconstraints:].</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #800080;"><span style="color: #800080;">3</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) removeconstraint: (nslayoutconstraint *) constraint Ns_available_ios (6_0);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">this <span style="color: #008000;">method is deprecated in a future release and should is Avoided. Instead set Nslayoutconstraint ' s active property to NO.</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #800080;"><span style="color: #800080;">4</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) removeconstraints: (nsarray *) constraints Ns_available_ios (6_0);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">this <span style="color: #008000;">method is deprecated in a future release and should is Avoided. Instead Use +[nslayoutconstraint deactivateconstraints:].</span></span></pre><p><p>3, iOS8 new Add method, Activate or deactivate the specified constraint</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">the <span style="color: #008000;">receiver is activated or deactivated by manipulating.  Only active constraints affect the calculated Layout. Attempting to activate a constraint whose items has no common ancestor would cause an exception to be thrown. Defaults to NO for newly created Constraints. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>@property (getter=<span style="color: #000000;"><span style="color: #000000;">IsActive) BOOL Active ns_available (10_10, 8_0);</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">convenience method that activates each constraint in the contained array, in the same manner as setting Active=yes. T His was often more efficient than activating constraint individually. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>+ (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Activateconstraints: (nsarray *<span style="color: #000000;"><span style="color: #000000;">) Constraints ns_available (10_10, 8_0);</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">convenience method that deactivates each constraint in the contained array, in the same manner as setting active=no. This is often more efficient than deactivating each constraint individually. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>+ (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) deactivateconstraints: (nsarray *) constraints ns_available (10_10, 8_0);</pre><p><p></p></p><p class="p1"><p class="p1"><span class="s1">three, Coding Time</span></p></p><p class="p1"><p class="p1"><span class="s1">A> Setting the view View1 a square with a width of =20</span></p></p><p class="p1"><p class="p1"><span class="s1">Two ways of writing, the first width = 20, height =20</span></p></p><pre><pre><span style="color: #008080;">1</span> [self addconstraint:[nslayoutconstraint constraintwithitem:view1 attribute:nslayoutattributewidth relatedBy: Nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:<span style="color: #800080;">1.0</span> constant:<span style="color: #800080;">20 </span><span style="color: #000000;">]];</span> <span style="color: #008080;">2</span> [self addconstraint:[nslayoutconstraint constraintwithitem:view1 attribute:nslayoutattributeheight relatedBy: Nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:<span style="color: #800080;">1.0</span> constant:<span style="color: #800080;">20 </span>]];</pre></pre><p><p></p></p><p class="p1"><p class="p1"><span class="s1">Second width = 20, height = width</span></p></p><pre><pre><span style="color: #008080;">1</span> [self addconstraint:[nslayoutconstraint constraintwithitem:view1 attribute:nslayoutattributewidth relatedBy: Nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:<span style="color: #800080;">1.0</span> constant:<span style="color: #800080;">20 </span><span style="color: #000000;">]];</span> <span style="color: #008080;">2</span> [self addconstraint:[nslayoutconstraint constraintwithitem:view1 attribute:nslayoutattributeheight relatedBy: Nslayoutrelationequal toitem:view1 attribute:nslayoutattributewidth multiplier:<span style="color: #800080;">1.0</span> constant:<span style="color: #800080;">0</span>];</pre></pre><p><p></p></p><p class="p1"><p class="p1"><span class="s1">The advantage of the second approach is that if you want to modify the size of the view1, you only need to modify one place.</span></p></p><p class="p1"><p class="p1"><span class="s1">b> Set the view View1.frame.</span> <span class="s1">origin.x = view view2.frame.origin.x</span></p></p><pre><pre>Nslayoutconstraint *leftconstraint = [nslayoutconstraint constraintwithitem:view1 attribute:NSLayoutAttributeLeft Relatedby:nslayoutrelationequal toitem:view2 attribute:nslayoutattributeleft multiplier:<span style="color: #800080;">1.0</span> constant: <span style="color: #800080;"> 0</span><span style="color: #000000;">];/</span><span style="color: #008000;">/</span> <span style="color: #008000;">Legacy method</span> <span style="color: #008000;">//</span><span style="color: #008000;">[self addconstraint:leftconstraint];</span> <span style="color: #008000;">//</span> <span style="color: #008000;">New Method 1</span> <span style="color: #000000;"></span> <span style="color: #008000;">//</span> <span style="color: #008000;">New Method 2</span>leftconstraint.active = YES;</pre></pre><p><p></p></p><p><p>Nslayoutconstraint basics of automatic layout of iOS pages</p></p></span>

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.