iOS屏幕适配(尺寸适配)

屏幕尺寸适配:一ios

在.pch中加入如下代码,在定义每一个尺寸值的时候都调用下边的宏objective-c

 

//以iphone7为例  定义 view相关的宽高宏
#define
IPHONEHIGHT(b) [UIScreen mainScreen].bounds.size.height*((b)/1334.0) #define IPHONEWIDTH(a) [UIScreen mainScreen].bounds.size.width*((a)/750.0) //有导航栏和分栏的状况下高度 #define PhoneHight(d) ([UIScreen mainScreen].bounds.size.height-113.0)*((d)/1108.0) //仅仅有导航栏的状况下高度 #define PHONEHIGHT(d) ([UIScreen mainScreen].bounds.size.height-64.0)*((d)/1206.0) #define ScreenWidth CGRectGetWidth([UIScreen mainScreen].bounds) #define ScreenHeight CGRectGetHeight([UIScreen mainScreen].bounds)

 

 

 

 

 

 屏幕尺寸适配:二iphone

建立项目-建立PCH文件:若要建立.pch , 在other里选择 PCH file,并须要修改一下设置。在build settings 里设置 Precompile Prefix Header的值为YES,并设置Prefix Header的路径。ui

建立一个扩展文件UIView+CreaFream.h,(如何建立扩展文件:建立一个objective-c file , 能够选择 category, extension ,protocol, empty 文件。选category 就能创建类别。)再不会了看网址 http://blog.csdn.net/idoshi201109/article/details/51735461spa

 

擦,差点忘了 PCH里边的东西了.net

.pchcode

#ifndef PrefixHeader_pch
#define PrefixHeader_pch

//判断ios版本
#define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0 )


#import "UIView+CreaFream.h"

#define IPHONE4 ScreenRect.size.width ==320 && ScreenRect.size.height ==480

#define IPHONE5 ScreenRect.size.width ==320 && ScreenRect.size.height ==568

#define IPHONE6 ScreenRect.size.width ==375 && ScreenRect.size.height ==667

#define IPHONE6p ScreenRect.size.width ==414 && ScreenRect.size.height ==736

//#define iPhone7 iPhone7P 本身补充,不过和6的尺寸同样的


#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS




#endif /* PrefixHeader_pch */

 

.hblog

#import <UIKit/UIKit.h>

@interface UIView (CreaFream)

//只需一个设置间隔 大小的方法
+(CGRect)GetRectWithX:(CGFloat)x Y:(CGFloat)y Width:(CGFloat)W Heigth:(CGFloat)H;

@end

  

.mip

#import "UIView+CreaFream.h"

@implementation UIView (CreaFream)

//实现方法
+(CGRect)GetRectWithX:(CGFloat)x Y:(CGFloat)y Width:(CGFloat)W Heigth:(CGFloat)H{
    
    CGRect temptect = CGRectZero;
   
//我的喜欢以iPhone 为基准 进行比例大小的对比

    if (IPHONE6) {
        temptect = CGRectMake(x,y, W, H);
        
    }else if ((IPHONE4)||(IPHONE5)){
        
        CGFloat Xscole = 320/375.0;
        CGFloat Yscole = 480/667.0;
        
        temptect = CGRectMake(x*Xscole, y*Yscole, W*Xscole, Yscole*H);
        
        
    }else if (IPHONE6p){
        
        CGFloat Xscole = 414/375.0;
        CGFloat Yscole = 736/667.0;
        
        temptect = CGRectMake(x*Xscole, y*Yscole, W*Xscole, Yscole*H);
        
    }
   
    
    return temptect;
}
@end

  

 

就是这样了,在使用的时候,具体就是这样的it

 

//在建立基于 UIView 的试图控件的时候,Frame先不设定,如在建立一个UIImageView的时候:frame 之间用UIView 调用,就能够适应其余屏幕了

 UIImageView * imgView = [[UIImageView alloc] initWithFrame:[UIView GetRectWithX:30 Y:30 Width:100 Heigth:100]];
        
相关文章
相关标签/搜索