標籤:
首先說下讓自己的程式支援iPhone6和6+,第一種使用官方提供的launch screen.xib,這個直接看官方文檔即可,這裡不再多述;第二種方法是和之前iPhone5的類似,比較簡單,為iPhone6和6+添加兩張特殊的png
iPhone6:命名:
解析度:7501334
6+ 命名:
解析度:12422208
注意:
如果要在app的介紹頁面裡有“為iPhone6,6 plus最佳化”的字樣就必須使用第一種方法,使用第二種方法的話還是會顯示“為iPhone5最佳化”
下面說一下純程式碼適配
首先iPhone5的介面一定要完全適配,這樣才能完美適配6和6Plus。
首先,我麼我們要觀察一下5,6和6Plus的尺寸比例關係
很明顯能看出這三種螢幕的尺寸寬高比是差不多的,因此可以在5的基礎上,按比例放大來適配6和6Plus的螢幕。
在AppDelegate.h中
@property float autoSizeScaleX;
@property float autoSizeScaleY;
在AppDelegate.m中
define ScreenHeight [[UIScreen mainScreen] bounds].size.heightdefine 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檔案中
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的或者其他的手動調整一下即可。
iOS純程式碼工程手動快速適配