小談iOS螢幕適配問題

來源:互聯網
上載者:User

標籤:

首先,我麼我們要觀察一下5,6和6Plus的尺寸比例關係。發現了他們的關係後待會做相容就明白了。

很明顯能看出這三種螢幕的尺寸寬高比是差不多的,因此可以在5的基礎上,按比例放大來相容6和6Plus的螢幕。
在AppDelegate.h中

12 @property float autoSizeScaleX;@property float autoSizeScaleY;

在AppDelegate.m中

1234567891011121314 #define ScreenHeight [[UIScreen mainScreen] bounds].size.height//擷取螢幕高度,相容性測試#define ScreenWidth [[UIScreen mainScreen] bounds].size.width//擷取螢幕寬度,相容性測試  - (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;    }}

因為iPhone4s螢幕的高度是480,因此當螢幕尺寸大於iPhone4時,autoSizeScaleX和autoSizeScaleY即為當前螢幕和iPhone5尺寸的寬高比。比如,
如果是5,autoSizeScaleX=1,autoSizeScaleY=1;
如果是6,autoSizeScaleX=1.171875,autoSizeScaleY=1.17429577;
如果是6Plus,autoSizeScaleX=1.29375,autoSizeScaleY=1.2957;
現在我們擷取了比例關係後,先來看一下如何解決代碼設定介面時的相容。
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)這個方法使我們常用的設定尺寸的方法,現在我設定了一個類似於這樣的方法。
在.m檔案中

1234567891011 UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake1(100, 100, 50, 50)];  CG_INLINE CGRect//注意:這裡的代碼要放在.m檔案最下面的位置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;}

這樣,這個btn按鈕在5,6和6Plus的位置和尺寸比例都是一樣的。
代碼相容完之後,來看一下StoryBoard的相容,當然,在平時的項目中我們不可能就一兩個視圖,而且每個視圖裡面可定還套有很多其他視圖,如果把所有視圖的尺寸用手動輸入CGRectMake的方法來改相容的話工作量非常大,而且容易出錯。經過多次實驗,我想出一種能快速相容大多數介面的方法
在AppDelegate.m中

123456789101112131415161718192021 //storyBoard view自動適配+ (void)storyBoradAutoLay:(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);        }    }}  //修改CGRectMakeCG_INLINE CGRectCGRectMake1(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;}

storyBoradAutoLay是把當前view進行兩層遍曆,把裡面的UIView類型的控制項的尺寸取出來,乘以對應的比例再賦值給它的尺寸,這樣StoryBoard裡的相容就完成了。如果你的介面裡嵌套的比較多的話可以多加幾層遍曆。

在繼承了UIViewController的.m檔案中

12345 #import "AppDelegate.h"- (void)viewDidLoad{    [super viewDidLoad];    [AppDelegate storyBoradAutoLay:self.view];}

在所有的繼承了UIViewController的檔案中,都加上storyBoradAutoLay這句代碼,就能把當前的view進行相容。
現在我們來看一下使用了該方法相容的前後對比效果吧。

iPhone6相容前

iPhone6相容後

iPhone6Plus相容前

iPhone6Plus相容後

 如果整個項目做完後才開始做相容的話這個方法的優勢就體現出來了,面對幾十個工程檔案,只需自訂並且替換你的CGRectMake方法,再加上storyBoradAutoLay這個方法就瞬間完成大部分甚至全部的相容。
其實還是比較建議用代碼和StoryBoard結合的方式來寫代碼,無論是從做相容還是效率來說都是比較好的。如果遇到tableView的或者其他的相容改動,手動調整一下即可。

小談iOS螢幕適配問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.