iOS Development-Auto layout autoresizing and AutoLayout

Source: Internet
Author: User

There are two types of automatic layouts for Apple:
(1) autoresizing
(2) AutoLayout
The following first describes autoresizing:
Before we get started, we're going to set up Xcode because the default Xcode is open AutoLayout, and you can open autoresizing with the following two images.

Then follow, select the corresponding tab to be able to use the autoresizing function.

Now do an exercise with autoresizing, so that a control is always in the lower-right corner of the parent control, such as setting the corresponding view: (Click to cancel or add)

This will do the work. Outside the box is the margin of the control and the parent control, and the small box inside is the width height of the set control.
Of course we can also set the autoresizing of the control through the code. Like the above effect we need to add the following code:

self.startButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth;

Where Autoresizingmask corresponds to the value, here is an introduction:

typedefNs_options (Nsuinteger, uiviewautoresizing) {Uiviewautoresizingnone =0,//The bottom four is the one that sets the direction of the autoresizing is not fixed, that is, in the panel where this line is not red. Uiviewautoresizingflexibleleftmargin =1<<0,//For example, setting this property means that the left side of the line is not selectedUiviewautoresizingflexiblerightmargin =1<<2, Uiviewautoresizingflexibletopmargin =1<<3, Uiviewautoresizingflexiblebottommargin =1<<5Uiviewautoresizingflexiblewidth =1<<1,//Width follows the width change of the parent controlUiviewautoresizingflexibleheight =1<<4,//height follows the height change of the parent control};

This setting is consistent with the effect you just needed. No matter how the screen rotates, it is in the lower right corner.

But one drawback of autoresizing is that he is used to set a relationship with the parent control. If you encounter a relationship that requires the control of two siblings to be set, autoresizing cannot do it, and we will be exposed to AutoLayout.

Xcode default to create a project is to support AutoLayout. You can set the corresponding properties of AutoLayout in the following location:

Following this setting, you will be able to get the same results as the above requirements:

Remember to select the value of update frames, otherwise the control will still be in the original position, actually run he would change the position, but in Xcode there is no direct refresh.

Of course, we can also set AutoLayout to achieve the above effect through the code:

[ Self. Startbuttonsetbackgroundcolor:[UicolorRedcolor]];//This sentence must be set     Self. Startbutton. Translatesautoresizingmaskintoconstraints=NO;//Add width constraintNslayoutconstraint *widthconstraint = [Nslayoutconstraint constraintwithitem: Self. StartbuttonAttribute:nslayoutattributewidth relatedby:nslayoutrelationequal Toitem:NilAttribute:nslayoutattributenotanattribute multiplier:0.0constant -]; [ Self. StartbuttonAddconstraint:widthconstraint];//Add height constraintNslayoutconstraint *heightconstraint = [Nslayoutconstraint constraintwithitem: Self. StartbuttonAttribute:nslayoutattributeheight relatedby:nslayoutrelationequal Toitem:NilAttribute:nslayoutattributenotanattribute multiplier:0.0constant -]; [ Self. StartbuttonAddconstraint:heightconstraint];//Add right constraintNslayoutconstraint *rightconstraint = [Nslayoutconstraint constraintwithitem: Self. StartbuttonAttribute:nslayoutattributeright relatedby:nslayoutrelationequal Toitem: Self. ViewAttribute:nslayoutattributeright multiplier:1.0constant0]; [ Self. ViewAddconstraint:rightconstraint];//Add bottom constraintNslayoutconstraint *bottomconstraint = [Nslayoutconstraint constraintwithitem: Self. StartbuttonAttribute:nslayoutattributebottom relatedby:nslayoutrelationequal Toitem: Self. ViewAttribute:nslayoutattributebottom multiplier:1.0constant0]; [ Self. ViewAddconstraint:bottomconstraint];

You see that you have to hit so much code to do this.
This is probably the initial introduction of these two automatic layouts.

iOS Development-Auto layout autoresizing and AutoLayout

Related Article

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.