Use autolayout (1) in code-proportional scaling and priority

Source: Internet
Author: User

First, scale proportionally, which cannot be set in interface builder. In the codeMultiplierYou can easily set proportional scaling. At the same time, you can set different nslayoutattribute parameters to achieve unexpected results, for example, "the width of a is twice the height of B.

OK. When writing the code, we will take a simple uibutton as an example and create a uibutton field in viewcontroller:

UIButton *btn;

 

The horizontal center of the button, which is always 20 units away from the bottom of the parent view. Then the height is 1/3 of the parent view height.

Finally, KVO is used to monitor the button size and output it to the screen in real time.

Code:

-(Void) viewdidload
{
[Super viewdidload];

// Create uibutton Without Frame
BTN = [uibutton buttonwithtype: uibuttontyperoundedrect];
[BTN settitle: @ "mgen" forstate: uicontrolstatenormal];
BTN. backgroundcolor = [uicolor greencolor];
[Self. View addsubview: BTN];

// Disable automatic conversion of autoresizingmask
BTN. translatesautoresizingmaskintoconstraints = no;

// Center
[Self. View addconstraint: [nslayoutconstraint
Constraintwithitem: BTN
Attribute: nslayoutattributecenterx
Relatedby: nslayoutrelationequal
Toitem: Self. View
Attribute: nslayoutattributecenterx
Multiplier: 1
Constant: 0];

// 20 units to the bottom
// Note that the constant created by nslayoutconstraint is added to the toitem parameter, So-20 is required.
[Self. View addconstraint: [nslayoutconstraint
Constraintwithitem: BTN
Attribute: nslayoutattributebottom
Relatedby: nslayoutrelationequal
Toitem: Self. View
Attribute: nslayoutattributebottom
Multiplier: 1
Constant:-20];

// Defines that the height is 1/3 of the parent View
[Self. View addconstraint: [nslayoutconstraint
Constraintwithitem: BTN
Attribute: nslayoutattributeheight
Relatedby: nslayoutrelationequal
Toitem: Self. View
Attribute: nslayoutattributeheight
Multiplier: 0.3
Constant: 0];

// Register the KVO Method
[BTN addobserver: Self forkeypath: @ "bounds" Options: nskeyvalueobservingoptionnew | nskeyvalueobservingoptioninitial context: Nil];
}

// KVO callback
-(Void) observevalueforkeypath :( nsstring *) keypath ofobject :( ID) object change :( nsdictionary *) change context :( void *) Context
{
If (Object = BTN & [keypath isw.tostring: @ "bounds"])
{
[BTN settitle: nsstringfromcgsize (BTN. bounds. Size) forstate: uicontrolstatenormal];
}
}

 

Running result:

 

 

OK, no problem.

 

Next, there is a new requirement. In the horizontal display, the height of the button is only 96 and it is too short. Therefore, the minimum height of the button must be 150.

In this case, we need to add another constraint with a limited size, but the two constraint conflicts in some cases. We can solve this problem by setting the constraint priority. The priority corresponds to the priority attribute of the nslayoutconstraint type. The default value is uilayoutpriorityrequired, and the value is 1000. Setting a low value indicates a lower priority.

In addition, nslayoutrelationgreaterthanorequal is used as the relatedby parameter when nslayoutconstraint type is created.

 

Modify the preceding ratio constraint and add a new minimum limit constraint below. Code:

// Defines that the height is 1/3 of the parent View
// Set the priority to lower than uilayoutpriorityrequired (1000), and uilayoutprioritydefaulthigh to 750
Nslayoutconstraint * con = [nslayoutconstraint
Constraintwithitem: BTN
Attribute: nslayoutattributeheight
Relatedby: nslayoutrelationequal
Toitem: Self. View
Attribute: nslayoutattributeheight
Multiplier: 0.3
Constant: 0];
Con. Priority = uilayoutprioritydefaulthigh;
[Self. View addconstraint: con];

// Set the minimum BTN height to 150
[BTN addconstraint: [nslayoutconstraint
Constraintwithitem: BTN
Attribute: nslayoutattributeheight
Relatedby: nslayoutrelationgreaterthanorequal
Toitem: Nil
Attribute: nslayoutattributenotanattribute
Multiplier: 1
Constant: 150];

 

After running, the height of the button in the horizontal screen is 150:

 

 

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

 

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.