1, 以iPhone5s爲根基的代碼自動佈局後續版本全適配.html
擴展連接(關於iPhone的各版本像素點以及iOS8,Xcode6的一些描述):ios
http://www.cocoachina.com/ios/20140912/9601.htmlapp
(1)引入數據ide
iPhone4s 480/320 ——— 1.5佈局
iPhone5s 568/320 ———1.775atom
iPhone6 667/375 ——— 1.779spa
iPhone6plus 736/414 ———1.778code
觀察能夠發現規律,排除iPhone4s ,後續的iPhone版本高寬比例大體都處在1.77 - 1.78這個範圍.(就目前而言).因而就產生了以iPhone5s爲根基的代碼自動佈局後續版本全適配.htm
(2)關鍵代碼段blog
AppDelegate.h
1 #import <UIKit/UIKit.h>
2
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4
5 @property (strong, nonatomic) UIWindow *window; 6
7 @property float autoSizeScaleX; 8 @property float autoSizeScaleY; 9
10 + (void)storyBoradAutoLay:(UIView *)allView; 11 @end
1 #define ScreenHeight ([[UIScreen mainScreen] bounds].size.height)
2
3 #define ScreenWidth ([[UIScreen mainScreen] bounds].size.width)
4
5 #import "AppDelegate.h"
6
7 @interface AppDelegate () 8
9 @end
10
11 @implementation AppDelegate 12 //此處的類方法實現須要注意,若是當前的view層次結構比較複雜,則應該多嵌套幾回循環
13 + (void)storyBoradAutoLay:(UIView *)allView 14 { 15 for (UIView *temp in allView.subviews) { 16 temp.frame = CGRectMake1(temp.frame.origin.x, temp.frame.origin.y, temp.frame.size.width, temp.frame.size.height); 17 for (UIView *temp1 in temp.subviews) { 18 temp1.frame = CGRectMake1(temp1.frame.origin.x, temp1.frame.origin.y, temp1.frame.size.width, temp1.frame.size.height); 19 } 20 } 21 } 22
23 CG_INLINE CGRect 24 CGRectMake1(CGFloat x, CGFloat y, CGFloat width, CGFloat height) 25 { 26 AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate]; 27 CGRect rect; 28 rect.origin.x = x * myDelegate.autoSizeScaleX; rect.origin.y = y * myDelegate.autoSizeScaleY; 29 rect.size.width = width * myDelegate.autoSizeScaleX; rect.size.height = height * myDelegate.autoSizeScaleY; 30 return rect; 31 } 32
33 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 34 // Override point for customization after application launch.
35
36 AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate]; 37
38 myDelegate.autoSizeScaleX = ScreenWidth/320; 39 myDelegate.autoSizeScaleY = ScreenHeight/568; 40
41
42 return YES; 43 }
(3)使用時只要在有需求的.m文件導入Appdelegate.h,引用storyBoradAutoLay:類方法便可
[AppDelegate storyBoradAutoLay:self.view];