Storyboard and code-in-proportion fast compatible IPHONE6/6 plus simple tutorials

Source: Internet
Author: User

The method in this article has a lot of limitations, please use it carefully!

Now because Apple is out of 6 and 6Plus, let the guy who writes the Apple program have a headache in order to do compatibility. It is convenient to use storyboard, but it takes too much time and energy to do compatibility later.
The use of AutoLayout is automatically laid out in different sizes of screens, but many things have to be manually modified, and with AutoLayout, there is a drawback that you cannot modify the size and position of controls on storyboard by code.
The use of pure code to build the interface will not be intuitive enough to spend time to adjust the layout, although it is convenient to adjust the compatibility later, but the impact of development efficiency.
Of course, personally think the code and storyboard combination of the way more convenient.
First of all, the requirements of using this method, first IPhone5 interface must be fully compatible, so as to be perfectly compatible with 6 and 6Plus.
First of all, let's look at the size-proportional relationship between 5,6 and 6Plus. After discovering their relationship, it is clear that they will be compatible later.

It is obvious that the size aspect ratio of the three screens is similar, so you can scale up to 6 and 6Plus on a 5 basis.
In the AppDelegate.h

1 float Autosizescalex; 2 float Autosizescaley;

In the APPDELEGATE.M

1 #definescreenheight [[UIScreen Mainscreen] Bounds].size.height//get screen height, compatibility test2 #definescreenwidth [[UIScreen Mainscreen] Bounds].size.width//get screen width, compatibility test3   4-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {5Appdelegate *mydelegate = [[UIApplication sharedapplication]Delegate];6       7     if(ScreenHeight >480){8Mydelegate.autosizescalex = screenwidth/ the;9Mydelegate.autosizescaley = screenheight/568;Ten}Else{ OneMydelegate.autosizescalex =1.0; AMydelegate.autosizescaley =1.0; -     } -}


Because the height of the iphone4s screen is 480, when the screen size is greater than iPhone4, Autosizescalex and Autosizescaley are the aspect ratios of the current screen and iPhone5 dimensions. Like what
If it is 5,autosizescalex=1,autosizescaley=1;
If it is 6,autosizescalex=1.171875,autosizescaley=1.17429577;
If it is 6plus,autosizescalex=1.29375,autosizescaley=1.2957;
Now that we have the proportional relationship, let's take a look at how to solve the code-setting interface compatibility.
CGRectMake (CGFloat x, cgfloat y, cgfloat width, cgfloat height) This method makes it common for us to set the size of the method, and now I set up a method similar to this one.
In the. m file

1UIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake1 ( -, -, -, -)];2   3Cg_inline CGRect//Note: The code here should be placed at the bottom of the. m file4 CGRectMake1 (CGFloat x, cgfloat y, cgfloat width, cgfloat height)5 {6Appdelegate *mydelegate = [[UIApplication sharedapplication]Delegate];7 CGRect rect;8rect.origin.x = x * MYDELEGATE.AUTOSIZESCALEX; RECT.ORIGIN.Y = y *Mydelegate.autosizescaley;9Rect.size.width = width * MYDELEGATE.AUTOSIZESCALEX; Rect.size.height = Height *Mydelegate.autosizescaley;Ten     returnrect; One}

In this way, the BTN button is in the same position and size ratio as the 5,6 and 6Plus.

After the code is compatible, look at the compatibility of storyboard, of course, in the normal project we can not be one or two views, and each view within the set has a lot of other views, if the size of all the views of the manual input CGRectMake method to be compatible with the workload is very large, and error prone. After many experiments, I came up with a way to quickly compatible with most interfaces.
In the APPDELEGATE.M

1 //StoryBoard View automatic adaptation2+ (void) Storyboradautolay: (UIView *) AllView3 {4      for(UIView *tempinchallview.subviews) {5Temp.frame =CGRectMake1 (temp.frame.origin.x, TEMP.FRAME.ORIGIN.Y, Temp.frame.size.width, temp.frame.size.height);6          for(UIView *TEMP1inchtemp.subviews) {7Temp1.frame =CGRectMake1 (temp1.frame.origin.x, TEMP1.FRAME.ORIGIN.Y, Temp1.frame.size.width, temp1.frame.size.height);8         }9     }Ten } One    A //Modify CGRectMake - Cg_inline CGRect - CGRectMake1 (CGFloat x, cgfloat y, cgfloat width, cgfloat height) the { -Appdelegate *mydelegate = [[UIApplication sharedapplication]Delegate]; - CGRect rect; -rect.origin.x = x * MYDELEGATE.AUTOSIZESCALEX; RECT.ORIGIN.Y = y *Mydelegate.autosizescaley; +Rect.size.width = width * MYDELEGATE.AUTOSIZESCALEX; Rect.size.height = Height *Mydelegate.autosizescaley; -     returnrect; +}


Storyboradautolay is the current view of the two-layer traversal, the UIView type of the inside of the size of the control to take out, multiplied by the corresponding scale and then assigned to its size, so that the compatibility of storyboard is completed. You can add a few more layers of traversal if you have more than one nested in your interface.

In the. m file that inherits the Uiviewcontroller

1 #import " AppDelegate.h " 2 -(void) viewdidload{3    [Super Viewdidload]; 4     [Appdelegate StoryBoradAutoLay:self.view]; 5 }

In all the files that inherit the Uiviewcontroller, add the code of Storyboradautolay to make the current view compatible.

Now let's take a look at the pre-and post-contrast effects that are compatible with this method.

IPhone6 compatibility before

IPhone6 after compatibility

Iphone6plus compatibility before

Iphone6plus after compatibility

If the whole project is done before you start to do compatibility, the advantage of this method is reflected in the face of dozens of project files, just customize and replace your CGRectMake method, plus Storyboradautolay This method to complete most or even all of the instant compatibility.
In fact, it is recommended to use code and storyboard combination of the way to write code, whether from the compatibility or efficiency is better.
If you encounter TableView or other compatible changes, adjust it manually.

Storyboard and code-in-proportion fast compatible IPHONE6/6 plus simple tutorials

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.