IOS: Use AutoLayout (2) –intrinsiccontentsize and content hugging priority "go" in code

Source: Internet
Author: User

Original: http://www.mgenware.com/blog/?p=491

Next: IOS: Use AutoLayout (1) in your code – scaled and prioritized.

We continue to look at the topic of using AutoLayout in code. First, that is intrinsicContentSize , the built-in size of the control. For example UILabel , the UIButton controls, they all have their own built-in size. The built-in size of the control is often determined by the contents of the control itself, such as a UILabel long text, and the UILabel built-in size is naturally very long. The built-in size of the control can be UIView obtained by the properties of the intrinsicContentSize built-in size, or it can be invalidateIntrinsicContentSize recalculated in the next UI planning event by means of a method intrinsicContentSize . If you create an original UIView object directly, it obviously has a built-in size of 0.

To continue writing AutoLayout in code, write an auxiliary method to quickly set UIView the margin limit:

//Set upAutoLayoutThe margin helper method in
- (void) Setedge: (UIView*) Superview View: (UIView*) View attr: (Nslayoutattribute) attr constant: (CGFloat) constant
{
[SuperviewAddConstraint:[nslayoutconstraintconstraintwithitem:view attribute:attr relatedby: toitem :superview attribute: attr multiplier:1.0< Span style= "color: #f3f3f3;" > constant:constant] ";
/span>

Next, create a uiview that uses the preceding helper method to quickly set its left, top, and right margin of 20 units on the parent control. The following code:

View1
UIView*view1 = [UIView New];
View1.BackgroundColor= [Uicolor Yellowcolor];
Do not allow autoresizingmask conversion to AutoLayout
View1.Translatesautoresizingmaskintoconstraints=NO;
[Self.View Addsubview: View1];
//Set left, top, right margin to20.
[Self Setedge:Self.View View: View1attr:Nslayoutattributeleft constant:20];
[Self Setedge:Self.View View: View1attr:Nslayoutattributetop constant:20 [self setedge:self.view view:view1 attr:nslayoutattributeright constant:-< span style = "COLOR: #cfab00;" >20

But after running it will be found that nothing is displayed on the interface. The reason is the above, the UIView default is not intrinsicContentSize . We can rewrite it by creating a custom one UIView intrinsicContentSize .

For example, create a new type: MyView.

Then overwrite the method in the. m file intrinsicContentSize and return a valid value, such as this:

rewrite UIView 's intrinsiccontentsize
-(cgsize) intrinsiccontentsize
{
returncgsizemake(+);
}

Then modify the topmost code to replace the type of the above View1 variable with UIView our custom View:myview type:

MyView *view1 = [MyViewNew];   

Run the code again and view will appear on the screen as required:

Next, in the same way, add another myview below, asking for its distance from the parent control margin to the left, bottom, and right 20, code:

View2
MyView*VIEW2 = [MyView New];
View2.BackgroundColor= [Uicolor Yellowcolor];
Do not allow autoresizingmask conversion to AutoLayout
View2.Translatesautoresizingmaskintoconstraints=NO;
[Self.View Addsubview: View2];
//Set left, bottom, right margin to20.
[Self Setedge:Self.View View: View2attr:Nslayoutattributeleft constant:20];
[Self Setedge:Self.View View: View2attr:Nslayoutattributebottom constant:-< span style = "COLOR: #cfab00;" >20 [self setedge:self.view view:view2 attr:nslayoutattributeright constant:-< span style = "COLOR: #cfab00;" >20 /span>

This is done after the operation:

Next, add the spacing in the AutoLayout by code. Command View1 and View2 must be spaced 20 units, notice here requires view2 under view1 20 units, so create NSLayoutConstraint the View2 parameter in front. Also note that the attribute parameter of the VIEW2 is NSLayoutAttributeTop , and the attribute parameter of View1 is NSLayoutAttributeBottom :

//Set of twoViewUp and down spacing is20
[Self.View AddConstraint:[Nslayoutconstraint Constraintwithitem:view2 attribute:< Span style= "color: #5d7c9d;" >nslayoutattributetop relatedby :nslayoutrelationequal toitem:view1 :nslayoutattributebottom multiplier:1.0 constant:20]];

Operation Result:

OK, indeed, at this time View1 and view2 spaced 20 units each other, but View1 was stretched.

The next task is to do not let View1 stretch, but let View2 stretch it? Here you need to use the content hugging priority of the control, which is common in control properties in Xcode, such as:

The Content hugging priority represents the precedence of the control rejecting the extrusion. The higher the priority, the less likely the control will be stretched.

The following content Compression resistance priority represents the control's refusal to compress the built-in space. The higher the priority, the less likely the control's built-in space will be compressed. And here's the built-in space, that's what it says UIView intrinsicContentSize .

So, if we set a higher value for the content hugging priority of view1 (in the view above), then when AutoLayout encounters such a decision about who to stretch, View1 will not be stretched, View2 with a lower priority will be stretched.

You can UIView set the setContentHuggingPriority:forAxis control's content hugging priority directly by using the method, where the Foraxis parameter represents landscape and portrait, and in this case only the portrait needs to be set, so incoming UILayoutConstraintAxisVertical . Full sentence code:

improve View1 's Content hugging priority
Setcontenthuggingpriority:uilayoutprioritydefaulthighforaxis: Uilayoutconstraintaxisvertical];

Operation Result:

IOS: Use AutoLayout (2) –intrinsiccontentsize and content hugging priority "go" in code

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.