iOS苹果手机适配代码

本文介绍了一种在iOS应用中实现屏幕尺寸自适应的方法。通过AppDelegate中的代码调整UIView的frame属性,确保应用能在不同分辨率的设备上正常显示。具体包括AppDelegate.h/m文件中的屏幕尺寸判断与比例缩放设置,以及ViewController.m中的UI元素布局调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值