iphone5出來了,從不用適配的咱們也要像android同樣適配不一樣分辨率的屏幕了。android
公司產品新版本須要適配iphone5,通過一番折騰算是搞定了。下面分享給你們:
iphone5的屏幕分辨率:1136 x 640 也便是高度變成了568,程序啓動時咱們須要一張retina圖片命名爲Default-568h@2x.png。在咱們建立工程時xcode會默認爲咱們建立一個純黑色的圖片替換便可。
最新版的xcode都已支持iphone5調試:選中模擬器---->設備---->iphone(Retina 4-inch),稍等片刻就能夠切換到iphone5模擬器。
要適配iphone5須要將view的autosizing設置爲以下狀態:
固然還要確認選中另外一項
這一項默認會選中的,意思是自動縮放子視圖。
若是咱們的view沒有使用xib那咱們可使用代碼設置這些屬性:
[cpp]
self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin
| UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin
| UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;接下來設置子視圖(好比button,image等):
對應代碼:
[cpp]
autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
意思是將控件縮放時與父視圖左邊和頂部對應。能夠根據具體須要設置子控件的autorizingMask相應值。
咱們還能夠經過代碼手動改變iphone5下控件的大小或位置:
首先斷定一下設備是否爲iphone5:
[cpp]
#define DEVICE_IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height == 568)
#define DEVICE_IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height == 568) 接着咱們能夠在view初始化的時候改變frame:
[cpp]
if (DEVICE_IS_IPHONE5) {
[botton setFrame:CGRectMake(0, 450, 320, 440)];
}
if (DEVICE_IS_IPHONE5) {
[botton setFrame:CGRectMake(0, 450, 320, 440)];
}