iOS開(kāi)發(fā)之適配iphone5
隨著iphone5普及度增高,從不用適配的我們也要像android一樣適配不同分辨率的屏幕了。
公司產(chǎn)品新版本需要適配iphone5,經(jīng)過(guò)一番折騰算是搞定了。下面分享給大家:
iphone5的屏幕分辨率:1136 x 640 也即是高度變成了568,程序啟動(dòng)時(shí)我們需要一張retina圖片命名為Default-568h@2x.png。在我們創(chuàng)建工程時(shí)xcode會(huì)默認(rèn)為我們創(chuàng)建一個(gè)純黑色的圖片替換即可。
最新版的xcode都已支持iphone5調(diào)試:選中模擬器---->設(shè)備---->iphone(Retina 4-inch),稍等片刻就可以切換到iphone5模擬器。
要適配iphone5需要將view的autosizing設(shè)置為如下?tīng)顟B(tài):
當(dāng)然還要確認(rèn)選中另一項(xiàng)
這一項(xiàng)默認(rèn)會(huì)選中的,意思是自動(dòng)縮放子視圖。
如果我們的view沒(méi)有使用xib那我們可以使用代碼設(shè)置這些屬性:
- [cpp]
- self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
- | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin
- | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
- self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
- | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin
- | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
接下來(lái)設(shè)置子視圖(比如button,image等):
對(duì)應(yīng)代碼:
- [cpp]
- autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
- .autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
意思是將控件縮放時(shí)與父視圖左邊和頂部對(duì)應(yīng)??梢愿鶕?jù)具體需要設(shè)置子控件的autorizingMask相應(yīng)值。
我們還可以通過(guò)代碼手動(dòng)改變iphone5下控件的大小或位置:
首先判定一下設(shè)備是否為iphone5:
- [cpp]
- #define DEVICE_IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height == 568)
- #define DEVICE_IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height == 568) 接著我們可以在view初始化的時(shí)候改變frame:
- [cpp]
- if (DEVICE_IS_IPHONE5) {
- [botton setFrame:CGRectMake(0, 450, 320, 440)];
- }
- if (DEVICE_IS_IPHONE5) {
- [botton setFrame:CGRectMake(0, 450, 320, 440)];