<span id="Label3"></p> <ul> <li><p><p>Autoresizing</p></p> <ul> <ul> <li><p>The effect of the four wires around 1.Autoresizing:</p> <ul> <li>As long as you tick on a certain root, the current control distance from the parent control is fixed, what is the current, how much will always be</li> </ul></li> <li><p>2.Autoresizing the function of the middle two lines:</p> <ul> <li>As long as you tick the horizontal line, the width of the current control is stretched as the width of the parent control</li> <li>As long as the vertical line is ticked, the height of the current control is stretched as the height of the parent control</li> </ul></li> <li><p>3. Whether you are pinning a child control to a location in a parent control or having a child control change with the height of the parent control</p></li> <li><p>4 are both Parent-child relationships, so autoresizing can only constrain relationships between parent-child controls, and cannot constrain relationships between sibling controls</p></li> </ul> </ul> <ul> <li><p><p>Property</p></p><pre><code class="lang-objc"><code class="lang-objc"> <span class="hljs-keyword"> @property (<span class="hljs-keyword">nonatomic) Uiviewautoresizing autoresizingmask; <span class="hljs-comment">//simple resize. default is Uiviewautoresizingnone<span class="hljs-comment">// To set the autoresizing of a child control note: in the code, the left side of the current control and the parent control are fixed by checking the left side of the storyboard in the upper and lower left and Storyboard. In code, if you write flexibleleftmargin, it means that the left side of the current control and the parent control is stretched, in other words: if you set the flexibleleftmargin, it means that the right side is Fixed. Uiviewautoresizingflexibleleftmargin <span class="hljs-comment">//on The left can be scaled uiviewautoresizingflexiblerightmargin Span class= "hljs-comment" >//right can be scaled uiviewautoresizingflexibletopmargin <span class="hljs-comment">//top can be scaled Uiviewautoresizingflexiblebottommargin <span class="hljs-comment">//bottom can be scaled <span class="hljs-comment">// The following two are the same as in storyboard uiviewautoresizingflexiblewidth <span class="hljs-comment">//width can be scaled Uiviewautoresizingflexibleheight <span class="hljs-comment">//height can be scaled </span> </span> </span> </span> </span> </span> </span></span></span></span></code></code></pre></li> </ul></li> <li><p><p> AutoLayout </p> </p> <ul> <ul> <li> <p> 1. constraint </p> <pre> <code> each add a setting in Storyboard (autolayout setting), which represents adding a constraint </code> </pre> </li> <li> <p> 2. Error (red Arrow) </p> <pre> <code> If you see a red arrow in the storyboard, there is an error in the constraint: there is an error in the constraint, it does not mean that the run error, the constraint has an error can also be run note: Red arrow is the programmer must resolve </code> </pre> <ul> <li> 3. Why is there a constraint error? <ul> <li> 3.1 missing constraint <ul> <li> Autolayout's nature and frame almost </li> <li> if you set a control through a frame, you must set the Control's x/y/w/h to Can be displayed according to our NEEDS. </li> <li> if you set a control by using autolayout, You must also set the Control's x/y/w/h, and the control will be displayed according to our requirements </li> <li> that is, if x/y/w/h has one Any settings will be error, is the lack of constraints </li> </ul> </li> <li> 3.2 constraint conflicts <ul> <li> constraints can be added repeatedly </li> <li> for example, the constraint width is equal to 100, and a bundle, constraint width, etc. 200, then the error will be </li> </ul> </li> </ul> </li> </ul> </li> <li> <p> 4. Notice that </p> <ul> <li> is at the top of the 20 = = equivalent to Setting the y </li> <li> 20 = = On the left is equivalent to setting x </li> </ul> </li> <li> <p> 5. Warning </p> <ul> <li> if you see a yellow arrow in storyboard, the warning </li> <li> represents the The position or size of the preview of the front control is not the same as the size of our constraint </li> <li> note: The gold warning does not affect our run </li> <li> note: Yellow arrow, programmer can ignore </li> </ul> </li> </ul> </ul></li> </ul><pre><pre><code class="lang-objc"> <span class="hljs-preprocessor">#warning 注意点: 如果通过代码来设置Autolayout约束, 那么必须先禁用Autoresizing redView<span class="hljs-variable">.translatesAutoresizingMaskIntoConstraints = <span class="hljs-literal">NO; Item == first item attribute == first item需要设置的约束类型 relatedBy == Relatio(等于) toItem == Second item attribute == Second item的约束类型 multiplier == 乘以 constant == 加上多少 <span class="hljs-comment">// 2.1宽度 NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:<span class="hljs-literal">nil attribute:kNilOptions multiplier:<span class="hljs-number">1.0 constant:<span class="hljs-number">100.0]; <span class="hljs-comment">// 将约束添加给自己 [redView addConstraint:width];</span></span></span></span></span></span></span></span></code></pre></pre> <ul> <li><p><p>VFL</p></p><pre><code class="lang-objc"> <span class="hljs-comment">1. Prohibition of red View autgoresizing blueveiw<span class="hljs-variable">. translatesautoresizingmaskintoconstraints =<span class="hljs-literal">NO; Redveiw<span class="hljs-variable">. translatesautoresizingmaskintoconstraints =<span class="hljs-literal">NO;<span class="hljs-comment">2. Adding constraints using the VFL<span class="hljs-comment">/* VISUALFORMAT:VFL Statement Options: Some of the controls used in METRICS:VFL statements, such as alignment, VIEWS:VFL statements */<span class="hljs-comment"><span class="hljs-comment">//3.1 Set Blue <span class="hljs-comment">//3.1.1 horizontal direction <span class="hljs-built_in">Nsarray *bluehcos = [nslayoutconstraint constraintswithvisualformat:<span class="hljs-string">@ "h:|-20-[ blueveiw]-20-| "options:kniloptions metrics:<span class="hljs-literal">nil views:@{<span class="hljs-string">@" blueveiw ": blueveiw}]; [<span class="hljs-keyword">self<span class="hljs-variable">. View addconstraints:bluehcos]; <span class="hljs-comment">//3.1.2 Vertical direction <span class="hljs-built_in">nsarray *bluevcos = [nslayoutconstraint constraintswithvisualformat:<span class="hljs-string">@ "v:|-20-[blueveiw (= = ] "options:kniloptions metrics:<span class="hljs-literal">nil views:@{<span class="hljs-string">@" blueveiw ": blueveiw}]; [<span class="hljs-keyword">self<span class="hljs-variable">. View addconstraints:bluevcos];</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></code></pre></li> <li><p><p>Masonry frame</p></p><p><p>"' OBJC</p></p><pre><code> Define this constant if you want to use masonry without the ' mas_ ' prefixdefine mas_shorthand//as long as you add this macro, you can remove the masonry frame The object accesses the Mas_ property before the object property, and the Mas_ prefix before the Method//such as Make.left.equalTo (self.view.mas_left) before Adding. with.offset (20);// For example, Add the following make.left.equalTo (self.view.left). With.offset//define This constant if you want to enable auto-boxing for Default Syntaxdefine mas_shorthand_globals//just Add this macro, give Equalto pass parameters, you can directly pass the basic data type, the system will automatically wrap//if not add the above macro, Then to pass the parameters to equalto, must pass the object//if you want to pass the base data type must use mas_equalto//only need to add the previous two macro before importing Masonry.h, you can simplify the code uiview *BLUEVEIW = [[ UIView alloc] init];blueveiw.backgroundcolor = [uicolor bluecolor]; [self.view addsubview:blueveiw];self.blueveiw = blueveiw; UIView *redveiw = [[UIView alloc] init];redveiw.backgroundcolor = [uicolor redcolor]; [self.view addsubview:redveiw];//2. autgoresizing blueveiw.translatesautoresizingmaskintoconstraints of Red view is forbidden; redveiw.translatesautoresizingmaskintoconstraints = no;//3. Add a blue constraint [blueveiw makeconstraints:^ (MASConstraintMaKer *make) {make.left.equalTo (self.view.left). offset (20); Make.right.equalTo (self.view.right). Offset (-20); Make.top.equalTo (self.view.top). Offset (20); Make.height.equalTo (50);}];/ /4. Add a red constraint [redveiw makeconstraints:^ (masconstraintmaker *make) {make.top.equalTo (blueveiw.bottom). offset (20); Make.height.equalTo (blueveiw.height); Make.right.equalTo (blueveiw.right); Make.width.equalTo (blueveiw.width). Multipliedby (0.5);</code></pre><p><p>}];</p></p><pre><pre><code>//注意: 在Storyboard中约束是可以重复添加的, 通过Masonry添加约束也是可以重复的, 要注意重复添加导致的错误和冲突// 使用makeConstraints, 每次都会添加新的约束, 也就是会导致重复添加// [self.blueVeiw makeConstraints:^(MASConstraintMaker *make) {// make.height.equalTo(100); }];// 要更新约束使用updateConstraints// updateConstraints特点: 如果没有设置过, 就添加一个新的 // 如果已经设置过了, 就更新以前设置的那一个 [self.blueVeiw updateConstraints:^(MASConstraintMaker *make) { make.height.equalTo(100); }];</code></pre></pre></li> </ul><pre><pre><code>// 清空约束 remakeConstraints[self.blueVeiw remakeConstraints:^(MASConstraintMaker *make) { }];</code></pre></pre><p><p>IOS UI Basics 06</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