First, iOS screen adaptation development process
Equipment |
Adaptation Technology |
4 and before (ipad not out ) |
Calculate directly with code |
With the ipad |
Autoresizing |
There are different screens on the iphone after |
AutoLayout |
There are more different screens on the iphone after |
Sizeclass |
Second, the characteristics of each technology 1, the direct use of code calculation
Because the screen size is the same, only the situation of the screen, you can directly calculate
2, Autoresizing
Appropriate for the relationship of the control to its parent control
Explanation of each attribute
Property |
explanation |
uiviewautoresizingnone | The
does not change with the parent view |
uiviewautoresizingflexibleleftmargin |
adjusts the left margin of the view and parent views automatically to ensure that the right margin is not changed |
uiviewautoresizingflexiblewidth |
automatically adjusts the width of the view to ensure that the left and right margins are not changed |
uiviewautoresizingflexiblerightmargin |
automatically adjusts the view to the right margin of the parent view to ensure that the left margin is not changed |
uiviewautoresizingflexibletopmargin |
automatically adjusts the top margin of the view and parent views to ensure that the bottom margin is not changed |
uiviewautoresizingflexibleheight |
automatically adjusts the height of the view to ensure that the top and bottom margins are not changed |
uiviewautoresizingflexiblebottommargin |
adjusts the bottom margin of the view and parent views automatically to ensure that the top margin is not changed |
- When the Autoresizessubviews property of view is Yes (default is YES), autoresizing will not take effect.
- Starting with XCODE6, Storyboard&xib is automatically layout by default, so we need to adjust it manually to use autoresizing.
- Autoresizing can be used in combination, such as:
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin
3, AutoLayout
Help us determine that the same visual unit should have the appropriate location and size in different devices and different (parent view) environments ( any relationship between the two views can be determined )
1. Usage of AutoLayout:
- Direct establishment of constraint conditions
[self.view addConstraint: [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
This way, although the code is relatively large, but it is absolutely feasible, but also the use of AutoLayout one of the most fundamental way.
- Using the VFL language
- (void) Viewdidload {[Super Viewdidload];UIButton *button=[[UIButton Alloc]init]; [Button Settitle:@ "click" forstate:UIControlStateNormal]; button. translatesautoresizingmaskintoconstraints=NO; [Button setbackgroundcolor:[Uicolor Blackcolor]]; [Self. View Addsubview:button];nsarray *constraints1=[nslayoutconstraint Constraintswithvisualformat:@ "h:|-[button]-|" Options:0 Metrics:nil views:nsdictionaryofvariablebindings (Button) ]; nsarray *constraints2=[nslayoutconstraint Constraintswithvisualformat:@ "V:|-20-[button (==30)]" options: 0 metrics:nil views: Nsdictionaryofvariablebindings (Button)]; [self.view addconstraints:constraints1]; [self.view addconstraints:constraints2];}
- Use third-party libraries, such as:masonry,uiview+autolayout...
2. Benefits of AutoLayout:
- You can basically not consider the 3.5-inch and 4-inch and the upcoming x.x-inch screen different resolution of the problem, you can finally not be in the Viewdidload method to determine the different resolutions, different controls should be placed where, or for different resolutions write different storyboard and xib;
- You can discard the code that calculates Tableviewcell, Uilabel height based on different words, because AutoLayout will help you calculate it automatically;
- If your layout in the horizontal screen vertical screen changes are not particularly large, you do not have to write two sets of code for the vertical, or write two storyboard/xib;
4, Sizeclass
In IOS8, the size classes feature is added, which is an abstraction of the size of all current iOS devices. Then we will only divide the width and height of the screen into three different situations: compact: Tight, Regular: loose, any: arbitrary.
This width and height of 331 integration, altogether 9 of the situation. As shown, for each case. We can set different layouts in each case (including constraints on controls, even if controls are displayed)
Sizeclass.png
Understanding of the Sizeclass:
The essence of sizeclass is to divide the iOS screen into different abstractions, which are different combinations of abstractions that correspond to different device screens. So, with Sizeclass, you can adapt all the screens to the same set of UIs. Note: All of these adaptations are implemented using AutoLayout, and Sizeclass are only responsible for providing different screen sizes.
iOS screen adaptation