苹果手机适配手写代码

一、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 () 
 <pre name="code" class="objc">
 @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]; 
 <pre name="code" class="objc">
 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];
 } 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值