Use autolayout (2)-intrinsiccontentsize and content hugging priority in the code

Source: Internet
Author: User

Next article: IOS: Use autolayout (1) in the code-proportional scaling and priority.

Let's continue to look at the topic of using autolayout in the code. The intrinsiccontentsize is the built-in size of the control. For example, uilabel, uibutton, and other controls all have their own built-in sizes. The built-in size of a control is usually determined by the content of the control. For example, if the text of A uilabel is very long, the built-in size of the uilabel is naturally very long. The built-in size of the control can be obtained through the intrinsiccontentsize attribute of uiview, or the intrinsiccontentsize can be recalculated in the next UI planning event using the invalidateintrinsiccontentsize method. If you directly create an original uiview object, the built-in size is obviously 0.

 

Continue to use code to write autolayout. First, write an auxiliary method to quickly set the margin limit of uiview:

// Sets the margin auxiliary method in autolayout
-(Void) setedge :( uiview *) superview view :( uiview *) view ATTR :( nslayoutattribute) ATTR constant :( cgfloat) Constant
{
[Superview addconstraint: [nslayoutconstraint constraintwithitem: View attribute: ATTR relatedby: nslayoutrelationequal toitem: superview attribute: ATTR multiplier: 1.0 constant: constant];
}

 

Next, create a uiview and use the preceding auxiliary method to quickly set it to the left, top, and right margin of the parent control in 20 units. The following code:

// View1
Uiview * view1 = [uiview new];
View1.backgroundcolor = [uicolor yellowcolor];
// Autoresizingmask cannot be converted to autolayout
View1.translatesautoresizingmaskintoconstraints = no;
[Self. View addsubview: view1];
// Set the left, top, and right distance to 20.
[Self setedge: Self. view: view1 ATTR: nslayoutattributeleft constant: 20];
[Self setedge: Self. view: view1 ATTR: nslayoutattributetop constant: 20];
[Self setedge: Self. view: view1 ATTR: nslayoutattributeright constant:-20];

 

However, you will find that nothing is displayed on the interface. The reason is as mentioned above, uiview does not have intrinsiccontentsize by default. You can create a custom uiview to rewrite the intrinsiccontentsize.

For example, create a new type: myview.

 

Then rewrite the intrinsiccontentsize method in the. M file and return the valid value, for example:

// Rewrite the intrinsiccontentsize of the uiview
-(Cgsize) intrinsiccontentsize
{
Return cgsizemake (70, 40 );
}

 

Next, modify the code at the top, and replace the view1 variable type from uiview with our custom view: myview type:

MyView *view1 = [MyView new];

 

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

 

Next, follow the same method to add another myview to the bottom, and the distance from the left, bottom, and right of the parent control is 20. Code:

// View2
Myview * view2 = [myview new];
View2.backgroundcolor = [uicolor yellowcolor];
// Autoresizingmask cannot be converted to autolayout
View2.translatesautoresizingmaskintoconstraints = no;
[Self. View addsubview: view2];
// Set left, bottom, and right to 20.
[Self setedge: Self. view: view2 ATTR: nslayoutattributeleft constant: 20];
[Self setedge: Self. view: view2 ATTR: nslayoutattributebottom constant:-20];
[Self setedge: Self. view: view2 ATTR: nslayoutattributeright constant:-20];

This is the case after running:

 

Next, add the spacing in autolayout through the code. The view1 and view2 commands must be separated between 20 units. Note that view2 requires 20 units under view1, so the view2 parameter in nslayoutconstraint creation must be in front. Note that the attribute parameter of view2 is nslayoutattributetop, and the attribute parameter of view1 is nslayoutattributebottom:

// Set the spacing between two views to 20
[Self. View addconstraint: [nslayoutconstraint constraintwithitem: view2 attribute: nslayoutattributetop relatedby: nslayoutrelationequal toitem: view1 attribute: extends multiplier: 1.0 constant: 20];

 

Running result:

 

OK. Indeed, view1 and view2 are separated by 20 units, but view1 is stretched.

The next task is to do how to make view1 stretch and make view2 stretch? Here we need to use the content hugging priority of the control, which is very common in the control attributes of xcode, such:

 

Content hugging priority indicates the priority of the control to reject stretching. The higher the priority, the less easily the control will be stretched.

The following content compression resistance priority indicates that the control rejects the priority of the built-in space. The higher the priority, the more difficult it is to compress the built-in space of the control. The built-in space here is the intrinsiccontentsize of the uiview mentioned above.

Therefore, if we set a higher value for the content hugging priority of view1 (in stretched, in the preceding view), when autolayout encounters such a situation that determines who to stretch, view1 will not be stretched first, while view2 with a lower priority will be stretched.

 

You can directly use the setcontenthuggingpriority: foraxis method of uiview to set the content hugging priority of the control. The foraxis parameter indicates the horizontal and vertical fields. In this example, you only need to set the vertical lines. The entire code:

// Improve the content hugging priority of view1
[View1 setcontenthuggingpriority: uilayoutprioritydefaulthweigh foraxis: uilayoutconstraintaxisvertical];

 

Running result:

 

 

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

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.