iOS純程式碼手動適配

來源:互聯網
上載者:User

iOS純程式碼手動適配

首先說下讓自己的程式支援iPhone6和6+,第一種使用官方提供的launch screen.xib,這個直接看官方文檔即可,這裡不再多述;第二種方法是和之前iPhone5的類似,比較簡單,為iPhone6和6+添加兩張特殊的png

iPhone6:命名:Default-375w-667h@2x.png 解析度:750*1334

6+ 命名:Default-414w-736h@3x.png 解析度:1242*2208

注意:

如果要在app的介紹頁面裡有“為iPhone6,6 plus最佳化”的字樣就必須使用第一種方法,使用第二種方法的話還是會顯示“為iPhone5最佳化”

 

下面說一下純程式碼適配

首先iPhone5的介面一定要完全適配,這樣才能完美適配6和6Plus。
首先,我麼我們要觀察一下5,6和6Plus的尺寸比例關係



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

 

在AppDelegate.h中

1

2

@property float autoSizeScaleX;

@property float autoSizeScaleY;

在AppDelegate.m中

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#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檔案中

1

2

3

4

5

6

7

8

9

10

11

UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake1(100, 100, 50, 50)];

 

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;

}

這樣,這個btn按鈕在5,6和6Plus的位置和尺寸比例都是一樣的。

 

如果整個項目做完後才開始做適配的話這個方法的優勢就體現出來了,面對幾十個工程檔案,只需自訂並且替換你的CGRectMake方法,再加上storyBoradAutoLay這個方法就瞬間完成大部分甚至全部的適配,如果遇到tableView的或者其他的手動調整一下即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.