auto Layout

1, 以iPhone5s为根基的代码自动布局后续版本全适配.

扩展链接(关于iPhone的各版本像素点以及iOS8,Xcode6的一些描述):

http://www.cocoachina.com/ios/20140912/9601.html

(1)引入数据

iPhone4s      480/320 ———  1.5

 

iPhone5s      568/320  ———1.775

iPhone6        667/375  ——— 1.779

iPhone6plus  736/414  ———1.778

观察可以发现规律,排除iPhone4s ,后续的iPhone版本高宽比例大致都处在1.77 - 1.78这个范围.(就目前而言).于是就产生了以iPhone5s为根基的代码自动布局后续版本全适配.

(2)关键代码段

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];

 

posted @ 2015-02-07 09:16  HHB17  阅读(148)  评论(0编辑  收藏  举报