iOS苹果手机适配代码

一、AppDelegate.h写法


#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;

//----------------------------开始------------------------------//

@property float autoSizeScaleX;

@property float autoSizeScaleY;

+ (void)iPhoneScreenAdaptation:(UIView *)allView;

//----------------------------结束------------------------------//

@end


二、AppDelegate.m写法


#import "AppDelegate.h"

#import "ViewController.h"

//----------------------------开始------------------------------//

#define ScreenHeight [[UIScreen mainScreen] bounds].size.height

#define ScreenWidth [[UIScreen mainScreen] bounds].size.width

//----------------------------结束------------------------------//

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //----------------------------开始------------------------------//

    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];

    

    if (ScreenHeight >480) {

        myDelegate.autoSizeScaleX = ScreenWidth/320;

        myDelegate.autoSizeScaleY = ScreenHeight/568;

    }else{

        myDelegate.autoSizeScaleX = 1.0;

        myDelegate.autoSizeScaleY = 1.0;

    }

    //----------------------------结束------------------------------//

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];

    ViewController *view = [[ViewController alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:view];

    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];

    

    return YES;

}


//----------------------------开始------------------------------//

+ (void)iPhoneScreenAdaptation:(UIView *)allView

{

    for (UIView *temp in allView.subviews) {

        temp.frame = CGRectMake1(temp.frame.origin.x, temp.frame.origin.y, temp.frame.size.width, temp.frame.size.height);

        for (UIView *temp1 in temp.subviews) {

            temp1.frame = CGRectMake1(temp1.frame.origin.x, temp1.frame.origin.y, temp1.frame.size.width, temp1.frame.size.height);

        }

    }

}


//修改CGRectMake

CG_INLINE CGRect

CGRectMake1(CGFloat x, CGFloat y, CGFloat width, CGFloat height)

{

    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;

    return rect;

}

//----------------------------结束------------------------------//


三、ViewController.m写法


#import "ViewController.h"

#import "AppDelegate.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self makeUI];

    //注意:布局在前,调用方法在后

    [AppDelegate iPhoneScreenAdaptation:self.view];

}


-(void)makeUI

{

    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(20, 64, 280, 44)];

    lab.backgroundColor = [UIColor redColor];

    [self.view addSubview:lab];

    

    UILabel *lab1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 64 +44+10, 300, 44)];

    lab1.backgroundColor = [UIColor greenColor]; 

    [self.view addSubview:lab1];

}

你可能感兴趣的:(ios,代码,手机,适配,苹果)