iOS屏幕適配(尺寸適配)


屏幕尺寸適配:一

在.pch中加入以下代碼,在定義每個尺寸值的時候都調用下邊的宏

 

//以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)

 

 

 

 

 

 屏幕尺寸適配:二

創建項目-創建PCH文件:若要創建.pch , 在other里選擇 PCH file,並需要修改一下設置。在build settings 里設置 Precompile Prefix Header的值為YES,並設置Prefix Header的路徑。

創建一個擴展文件UIView+CreaFream.h,(如何創建擴展文件:創建一個objective-c file , 可以選擇 category, extension ,protocol, empty 文件。選category 就能建立類別。)再不會了看網址 http://blog.csdn.net/idoshi201109/article/details/51735461

 

擦,差點忘了 PCH里邊的東西了

.pch

#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 */

 

.h

#import <UIKit/UIKit.h>

@interface UIView (CreaFream)

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

@end

  

.m

#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

  

 

就是這樣了,在使用的時候,具體就是這樣的

 

//在創建基於 UIView 的試圖控件的時候,Frame先不設定,如在創建一個UIImageView的時候:frame 之間用UIView 調用,就可以適應其他屏幕了

 UIImageView * imgView = [[UIImageView alloc] initWithFrame:[UIView GetRectWithX:30 Y:30 Width:100 Heigth:100]];
        

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM