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.

 


 

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.