IOS autoresizing Layouts

Source: Internet
Author: User

There are a variety of layouts for UIView and its sub-spaces, and today we have a look at the autoresizing layout

First, to understand the relevant knowledge:

1. UIView One of the properties is

@property (nonatomic) uiviewautoresizing autoresizingmask; Simple resize. Default is Uiviewautoresizingnone

This layout scheme is primarily a setting for this property

2, uiviewautoresizing for optional type ( ns_options, ns_enum slightly different )

typedef ns_options (Nsuinteger, uiviewautoresizing) {

Uiviewautoresizingnone = 0,//will not change with the parent view

Uiviewautoresizingflexibleleftmargin = 1 << 0,//automatically adjusts the left margin of the view and parent views to ensure that the right margin is constant

Uiviewautoresizingflexiblewidth = 1 << 1,//automatically adjusts the width of the view to ensure that the left and right margins are constant

Uiviewautoresizingflexiblerightmargin = 1 << 2,//automatically adjusts the view to the right margin of the parent view to ensure that the left margin is constant

Uiviewautoresizingflexibletopmargin = 1 << 3,//auto-adjust view and parent view top margin to keep bottom margin unchanged

Uiviewautoresizingflexibleheight = 1 << 4,//automatically adjusts the view height to ensure that the top and bottom margins are constant

Uiviewautoresizingflexiblebottommargin = 1 << 5//Adjusts the bottom margin of the view and parent views automatically to ensure that the top margin is not changed

};

Second, want to use this layout scheme on Xib or storyboard, to say that the default layout scheme (AutoLayout and size classes) is closed

It is important to note that this layout is used in pure code programming:

Autoresizing will take effect if the Autoresizessubviews property of UIView is Yes (default is YES).

Third, take xib as an example

A long time no use of xib, just use the process also appeared a little before encountered problems:

Loaded the "Testviewcontroller" nib but the view outlet is not set.

Workaround: Right-click the file ' s owner, and right-click the view item in the pop-up list to Xib (this view is set to the property of file ' owner), as

After a short break, continue to return:

The Autoresizingmask property of the orange control is set to a value of four uiviewautoresizingflexiblerightmargin, Uiviewautoresizingflexiblewidth, Uiviewautoresizingflexibleleftmargin, Uiviewautoresizingflexiblebottommargin (order left to right, top to bottom), with the aim of making the orange button's left margin, right margin, The top margin is fixed, the width is variable, and the height is constant, under any size screen!

This effect is displayed in code.

Self.view.autoresizesSubviews = YES;

CGFloat margin = 8;

CGFloat buttony = 65;

CGFloat height = 143;

CGFloat width = [UIScreen mainscreen].bounds.size.width-margin * 2;

UIButton *btn = [[UIButton alloc]initwithframe:cgrectmake (Margin, buttony, width, height)];

[Btn settitle:@ "autoresizing" forstate:uicontrolstatenormal];

[btn Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];

[Btn Setbackgroundcolor:[uicolor Orangecolor];

[Self.view ADDSUBVIEW:BTN];

autoresizing layout

Btn.autoresizingmask = uiviewautoresizingflexiblewidth| Uiviewautoresizingflexiblerightmargin | Uiviewautoresizingflexibleleftmargin | Uiviewautoresizingflexiblebottommargin;

Can show the same effect as Xib

In this process, there is a small problem, is the horizontal screen, vertical screen switching process, the height of the navigation bar changes, will lead to changes in the top margin (the effect appears to be the top margin changes),

Think about the solution, the code is as follows

Self.edgesforextendedlayout = Uirectedgenone; The navigation bar height is ignored when layout (attributes after iOS7)

This avoids the influence of the navigation bar height with the change of the screen and the layout!

   

IOS autoresizing Layouts

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.