如何应用autosizing属性使iOS应用程序界面适配iPhone5
另外两篇适配iphone5文章:
http://blog.csdn.net/linzhiji/article/details/8000048
http://blog.csdn.net/linzhiji/article/details/8000125
第一篇 iphone4程序适配iphone5方法,先安iphone4模拟器上的ui写程序所有空间都是ib脱的,什么也不用管。直到程序写好需要适配的时候在为每个xib文件复制一份改为iphone5的ui大小,重新布局,在对应类的.m文件里面initwithcoud:方法里面添加一个判断
在编码时,尽量不要写死480,根据屏当前屏幕([UIScreen mainScreen].bounds)选择size能在分辨率改变时省去不少麻烦。
stackoverflow上代码,如何判断iphone 5
[html] view plain copy
- #define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
如果要兼任ipad
[html] view plain copy
- <p class="p1">#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )<span class="s1">568</span> ) < DBL_EPSILON )</p><p class="p1">#define IS_IPHONE ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @<span class="s2">"iPhone"</span> ] )</p><p class="p1">#define IS_IPHONE_SIMULATOR ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @<span class="s2">"iPhone Simulator"</span> ] )</p><p class="p1">#define IS_IPOD ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @<span class="s2">"iPod touch"</span> ] )</p><p class="p1">#define IS_IPHONE_5 ( ((IS_IPHONE) || (IS_IPHONE_SIMULATOR)) && IS_WIDESCREEN )</p>
第二篇 1.判断当前设备是iPad还是iPhone\iPod
- if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
- NSLog(@"The Device is a iPad!");
- }else{
- NSLog(@"Not a iPad the device!");
- }
2.设置横屏或者竖屏或者横竖屏
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- //横屏
- //return UIInterfaceOrientationIsLandscape(interfaceOrientation);
- //竖屏
- //return UIInterfaceOrientationIsPortrait(interfaceOrientation);
- //横竖屏
- //return YES;
- }
iOS5和iOS6横竖屏同时支持
iOS6中抛弃了如下这个方法
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
为了同时支持iOS5和iOS6横竖屏切换,可用如下方法或代码
- 1 info.plist 中 Supported interface orientations中加入所有方向的支持
- 2 AppDelegate中加入方法
- -(NSUInteger)application:(UIApplication *)application
- supportedInterfaceOrientationsForWindow:(UIWindow *)window{
- return UIInterfaceOrientationMaskAll;
- } iOS6中为了后续支持任何方向的旋转
- 3 任何你想控制旋转的界面中加入方法
- // iOS6.0
- -(NSUInteger)supportedInterfaceOrientations{
- return UIInterfaceOrientationMaskPortrait; // 可以修改为任何方向
- }
- -(BOOL)shouldAutorotate{
- return YES;
- }
- // iOS5.0
- -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
- return (toInterfaceOrientation == UIInterfaceOrientationPortrait); // 可以修改为任何方向
- }
- 这样你的app就可以同时支持iOS5和iOS6系统的横竖屏切换了
3.导入旧工程,解决Xcode4.5以后模拟器屏幕不旋转问题
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0){
- self.window.rootViewController = navigationCtrl;
- }else{
- [self.window addSubview:navigationCtrl.view];
- }
4.支持iPhone5:添加Retina4 launch image"[email protected]"
图片尺寸:
- Default.png 320x480
- [email protected] 640x960
- [email protected] 640x1136
5.根据iPhone5的宽和高,判断当前设备是不是iPhone5
- CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
- CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
- if ((screenWidth==568)||(screenHeight==568)) {
- isiPhone5 = YES;
- }
注意:iPhone5的高是568,取得屏幕大小用
- [UIScreen mainScreen].bounds
第三篇 iPhone5 的屏幕为 320 * 568 ,而之前的 iPhone 均为 320 * 480 。由于屏幕大小的变化,使得原来开发的应用在 iPhone5 上面看的话,会留下上下两条黑边,除此之外,很多页面的布局也乱了。
实际上,最好的方法是,为iPhone5增加一个新的xib,重新布局UI,这样子才能根据iPhone5屏幕的分辨率来进行UI设计,也是最好的解决方法。但是这种方法耗费的时间成本太大。本文提供一个快速的适配iPhone5的方法。(无需监测设备型号,无需增加xib)。
1、消除上下黑边:如果你原来的程序是适应320*480的话,在iPhone5上面运行的结果如下,可以看到上下有两条黑边。如下图所示:


采用xcode4.5,编译你的程序,会有一行warnning,如下所示:


点击这个warnning,即弹出:


点击add按钮即可。
经过上面这个步骤之后,我们可以看到上下两条黑边不见了。
2、controller里面的view布局:我们看到运行在iPhone5上的程序,布局有些地方乱了。这是由于布局的原因引起的。每个从view继承下来的类,都有一个autosizing的属性,我们可以通过autosizing属性的调正来改变我们的布局来适配iPhone5。如下图:


我们可以看到,autosizing在xib中可以直接设置,图中显示的是上下左右四个方向的缩进,以及中间的上下拉伸属性。
我们先来看看上下左右的缩进,虚线表示按比例缩进,实线表示按于边界距离固定缩进。
中间的横向箭头如果为实线,则表示当前的view的宽度和其superView的宽度保持比例缩放;如果是虚线,则表示当前view的宽度保持不变。
中间的纵向箭头如果为实线,则表示当前的view的高度和其superView的高度保持比例缩放;如果是虚线,则表示当前view的高度保持不变。
我们在第一个步骤中,会将controller里面的view自动拉伸到iPhone5的分辨率。所以,我们的xib还是借用原来的xib不用改动。而对于view中的subviews,则要修改其autosizing的属性,以保证当view拉伸的时候,这些subviews也能跟着“动”起来。
3、如果有自定义的view并且是通过代码add到其他的view上去的话,则需要通过代码来设置autosizing的属性。autosizing有六种属性可以设置,分别如下:
UIViewAutoresizingFlexibleTopMargin:与superView上边界保持动态距离(按比例)
UIViewAutoresizingFlexibleBottomMargin:与superView下边界保持动态距离(按比例)
UIViewAutoresizingFlexibleLeftMargin:与superView左边界保持动态距离(按比例)
UIViewAutoresizingFlexibleRightMargin:与superView右边界保持动态距离(按比例)
UIViewAutoresizingFlexibleWidth:与superView宽度成比例
UIViewAutoresizingFlexibleHeight:与superView高度成比例
4、我们经常会在controller里面实现一个方法,即showInView,即把当前controller的view显示作为subview到其他view上去,如果你是想覆盖整个屏幕,则在showInView方法中应该使用下面语句来适配iPhone5:
[cpp] view plain copy
- (void)showInView:(UIView*)view
- {
- self.frame= view.bounds;
- // your codes.
- // ........
- }
同时,将controller里的view的autosizing设置成按宽度和高度拉伸的即可。
注意:在适配iPhone5的时候,要注意不要去监测设备的型号或者是设备类型,因为我们是对像素做适配,而不是对某种机型做适配。你可以使用[[UIScreen mainScreen] bounds]来获取当前设备屏幕大小。