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)];
}