Introduction to UI adaptation in IOS

Source: Internet
Author: User

It has been a long time since the iPhone 5 was released. Due to the changes in screen size, flexible UI control by the program is particularly important.

In fact, this summary still uses the relevant attributes in the old API, but we didn't think about it in depth before, or I didn't think about it in depth.

Let's start with viewcontroller's view (all the following code is in the arc environment). Manual view creation initializes viewcontroller's self. View from the loadview method. Here we will talk about the attributes in the API:

1. [uiscreen mainscreen]. bounds, bounds of the screen,

2. [uiscreen mainscreen]. applicationframe, App frame. When app statusbar is hidden, it is actually the same as [uiscreen mainscreen]. Bounds

I generally create a view as follows: Self. view = [[uiview alloc] initwithframe: [uiscreen mainscreen]. applicationframe];

In this case, the frame of the view is 0, 20, 320,548 on the iPhone 5, and the previous iPhone is 0, 20, 320,460,

Then the self of viewcontroller. the view frame is automatically changed in the viewwillappear method and automatically adapted to the screen size. That is to say, if you have navigationbar, The View frame is 320,504, (iphone5 ), I used to manually subtract 44 from loadview. I don't know if it's the same as mine...

Well, after talking so much, in a word, your controller's self. view will set its frame in viewwillappear to be filled with screen sizes except statusbar and navigationbar. If statusbar and navigationbar are absent or one of them is self. the frame of the view is still full of screens.

The following is an important adaptation attribute of uiview: autoresizingmask, which is used to determine how the position or size of a superview changes after the frame of a superview changes, its Attributes are as follows:

Enum {

Uiviewautoresizingnone = 0,

Uiviewautoresizingflexibleleftmargin = 1 <0,

Uiviewautoresizingflexiblewidth = 1 <1,

Uiviewautoresizingflexiblerightmargin = 1 <2,

Uiviewautoresizingflexibletopmargin = 1 <3,

Uiviewautoresizingflexibleheight = 1 <4,

Uiviewautoresizingflexiblebottommargin = 1 <5

};

Typedef nsuinteger uiviewautoresizing;

For example, if there is a button in your background, you want the button to be fixed at any time, regardless of the height at the bottom of the background, then you only need to set the autoresizingmask of the button to uiviewautoresizingflexibletopmargin.

Let's take a look at the definition of uiviewautoresizingflexibletopmargin: The view resizes by expanding or shrinking in the direction of the top margin. That is to say, in superview, the position from the top is variable.

If uiviewautoresizingflexibletopmargin | uiviewautoresizingflexiblebottommargin is set at the same time, the new position in the superview is the position after the frame of the superview changes) it is obtained by multiplying the old position by a proportional factor.

Proportional Factor: superview current height-its height/superview original height-its height

With these two knowledge points, you can easily adapt to iPhone 5 and the previous screen. For example, a certain viewb has an element, if you want a to be 20 px from the bottom of B in both iphone5 and 4S, you only need to set its autoresizingmask to uiviewautoresizingflexibletopmargin and Set B's new frame in the Controller's viewwillappear.

If you need to reprint, please indicate the source

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.