iOS Automatic layout Autoresizingi

Source: Internet
Author: User

For iOS app developers, there is no interface adaptation for many screen sizes like Android developers, so hard-coded coordinates work well, but it's not a good idea in design mode. There are also some problems, such as the adaptation of IPhone5, the switch between the screen and so on. Perhaps you can do two sets of UI solutions to fit, but this increases the duplication of effort, and not high-end, in case there is a new screen size. Philosophy will introduce two big automatic layouts in iOS: Autoresizing and Autolayout . Autoresizing is a UIView attribute, has always been, simple to use, but not autolayout powerful. AutoLayout is IOS6 after the new technology, more powerful. This article mainly describes Autoresizing the characteristics and usage.

1. Autoresizing Features

When UIView it autoresizesSubviews is YES , (by default yes), the child view in it autoresizingMask automatically adapts to its position and size according to its own properties superView .

autoresizingMaskis an enumeration type, which, by default UIViewAutoresizingNone , does not autoresize:

123456789       
typedefNs_options(Nsuinteger,Uiviewautoresizing){ Uiviewautoresizingnone=0, Uiviewautoresizingflexibleleftmargin=1<<0, Uiviewautoresizingflexiblewidth=1<< 1,  Uiviewautoresizingflexiblerightmargin = 1 << 2, uiviewautoresizingflexibletopmargin = 1 << 3, span class= "n" >uiviewautoresizingflexibleheight = 1 << 4, uiviewautoresizingflexiblebottommargin = 1 << 5                

This enumeration type, which is 1 << n defined by using this notation, represents the check that it can be. If you do not understand why, you can review the "bitwise Operation". So what do these values mean, respectively?

In fact, how to understand these values is very simple, that is, from the xib inside look. We uncheck in a xib file autolayout (AutoLayout is not visible by default when using the Autoresizing). Then we can see how to set it in the Layout column autoresizing .

Illustrates the relationship between these lines and the actual properties set in Xib, which should be noted that 4 margin dashed lines represent the value set, and that the width and height are solid lines that the value is set and cannot be taken for granted.

Each of these items represents:

Autoresizingmask is the left, right, top, bottom, and width and height of the child view proportionally to the parent view, for example:

Uiviewautoresizingnone does not adjust automatically.

Uiviewautoresizingflexibleleftmargin automatically proportionally adjusts the distance to the left of the Superview, and the distance to the right of the superview is unchanged.

Uiviewautoresizingflexiblerightmargin automatically proportionally adjusts the right distance to the Superview, and the distance to the left of the Superview is unchanged.

The uiviewautoresizingflexibletopmargin automatically proportionally adjusts the top distance to the Superview, and the distance from the bottom of the superview is unchanged.

The uiviewautoresizingflexiblebottommargin automatically proportionally adjusts the bottom distance to the Superview, and the distance from the top of the Superview is unchanged.

Uiviewautoresizingflexiblewidth automatically adjusts the width proportionally.

The uiviewautoresizingflexibleheight automatically adjusts the height proportionally.

uilabel* label = [[UILabel alloc] Initwithframe:cgrectmake (50, 100, 200, 40)];

[Label Setautoresizingmask:uiviewautoresizingnone]; Control does not change relative to parent view coordinate value

CGRectMake (uiviewautoresizingflexiblewidth): The width of the control changes proportionally with the width of the parent view, for example, the width of the 100 screen is 320 The label width changes to 100*480/320 when the screen width is 480

All of these are easier to understand, but autoresizing there are some combination scenarios. That is the combination of scenes.

Autoresizingmask Description xib Preview Effect
None The frame of view does not change with Superview (the preview effect in the xib of the right image is not the same as the actual effect, the actual effect is the top margin of the view is not changed)
TopMargin | BottomMargin The ratio of the top and bottom margins of a view to its superview remains the same
LeftMargin | RightMargin The ratio of the left and right margins of the view to its superview remains the same (the preview effect in the xib of the right image is not the same as the actual effect, and the actual effect is the top margin of the view remains the same)
LeftMargin | RightMargin | TopMargin | BottomMargin The ratio of the upper and lower left and right margins of the view and its superview remains unchanged
LeftMargin | Width The ratio of the right margin of the view to its Superview remains constant, with the left and width proportionally adjusted (the preview effect in the Xib on the right is not the same as the actual effect, the actual effect is that the top margin of the view remains the same)
LeftMargin | Width | RightMargin The left margin, the right margin, the wide proportional adjustment, (the right image of the xib in the preview effect and the actual effect is poor, the actual effect is the top margin of the view is not changed) the vertical direction is the same effect, it does not enumerate
Width | Height Automatically adjusts the width and height of the view to ensure that the upper and lower left and right margins are constant. If the TableView is set to this attribute, then no matter how large the Viewcontroller view is, it can be automatically filled

All the combination scenarios are not listed above, but they are enough for us to understand autoresizing .

2. Summary

The most common practical scenario for autoreszing is IPhone5 compatibility. For example, if we want to set tableview frame, we just need to set TableView's autoresizingmask to just after initializing set frame UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight .

Another example is that we want a view to stay at the bottom of its superview, so we just need to set the Autoresizingmask to be on after initializing the frame UIViewAutoresizingFlexibleTopMargin .

Autorezingmask a simple property, understanding it can make a lot of things easier.

iOS Automatic layout Autoresizingi

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.