IOS版本和IPHONE5/5S/6/6Plus适配总结

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

代码固定坐标方案

今天花了一天的时间对IOS6和ipHONE5进行适配 [由于自己用的机器是IPHONE5,所以没出什么问题,但是在IPHONE4上就出问题了,都是IOS7版本,还有一台IPOD是IOS6版本,也出问题~ 哎,一开始没注意适配,现在得花这精力去修改~ 特总结一下,防止以后犯错误,提高工作效率,加油!]

由于习惯问题,都在视图控制器下的viewDidLoad去创建自定义的UI元素,因为IOS版本问题,所以self.view.frame也会不同,如果要实现相同的UI布局,用代码进行测试,发现以下4种情况

3.5寸IPHONE

IPHONE5,IPHONE5S设备

结论:针对有导航栏的视图控制器,IOS6系统下self.view下的subviews的Origin.y是从导航栏底部开始计算的,高度是整个屏幕高度减去20后的值为参照,IOS7系统下是从0开始计算的,高度是整个屏幕高度为参照!

适配方案探讨:

1.在每个视图控制器的viewDidLoad去判断IOS版本和IPHONE尺寸,创建不同的frame,去设置每个UI元素 [当前由于时间问题,采取这种方案,发现略显繁琐,不易维护]

2.在每个视图控制器下加入以下代码:

1if([[[UIDevice currentDevice] systemVersion] floatValue] >=7) {2self.edgesForExtendedLayout =UIRectEdgeNone;3}

View Code

这样不管在IOS6还是IOS7下,元素的Origin.y都是一样的,不用再去分别设置了

然后只要控制每个元素的高度就行了,如下图:

3.在第二种方法前提下,在每个视图控制器下重写loadView方法

1-(void) loadView2{3[super loadView];4CGRect rect=[[UIScreen mainScreen]applicationFrame];5UIView* view=[[UIView alloc] initWithFrame:rect];6view.backgroundColor=[UIColor purpleColor];//根据项目需求,修改你自己的背景颜色7self.view=view;8[view release];9}

View Code

这样第二种方法里的高度设置都可以设置一样的,不用分别设置了~~

高度以屏幕高度减去20后的值为参照

最近遇到一些需求需要对6,6 plus以及iPad设备进行适配(也包括横竖屏的适配问题)

维护老项目(iPhone4S,iPhone5之前设备固定坐标UI布局的项目)在6和6 plus上使用:

http://www.cocoachina.com/ios/20141230/10800.html

iphone6 Plus下对应的屏幕frame

标准模式分辨率为1242x2208(iPhone6的2倍),放大模式分辨率为1125x2001

{{0, 0}, {414, 736}}

iphone6下对应的屏幕frame

分辨率是750x1334

{{0, 0}, {375, 667}}

iPad适配分辨率

基准为768*1024

设计和开发协作参考模式:http://www.cocoachina.com/ios/20141205/10534.html

如果按照传统的技术if..else代码进行布局(主要是frame和bounds),工作量会很大,也不利于后期维护

所以想起用自动布局技术,不过悲剧的事,项目需要从iOS5系统进行编码,最终使用autoresizing技术

ios6.x AutoResiziingMask适配

对于一些需要横竖屏适配的视图,适配规则为,保留尺寸不变,位置做相应处理(左右上下边距等比例缩小或放大)

不再需要针对ios7.x之前和ios8.x之后的坐标系,对目标视图计算不同的frame

[目标视图 setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin];

实例

适配目标:”描述性文字...“字样的UILabel边距为5 point,黄色图片矩形也是边距为5 point (不管横竖屏)

效果:

iPhone6

iPhone 6 plus

iPad retina

代码截图:(xcode 6.x ios8.1 sdk target: ios6 )

1-(void) testAutoresizing2{3//左边距20,上边距64,高度44, 位置水平居中4UIView* topView=[[UIView alloc] initWithFrame:CGRectMake(20,64, [UIScreen mainScreen].bounds.size.width -20*2,44)];5topView.layer.borderWidth=1.0;6topView.layer.borderColor=[UIColor blackColor].CGColor;7CGFloat textLabelTop=(topView.frame.size.width-200)/2;8CGFloat textLabelWidth=(topView.frame.size.height-50)/2;9//居中10UILabel* textLabel=[[UILabel alloc] initWithFrame:CGRectMake(textLabelTop, textLabelWidth,200,50)];11[textLabel setText:@"适配"];12[textLabel setAlpha:0.5];13[textLabel setBackgroundColor:[UIColor greenColor]];14[textLabel setTextAlignment:NSTextAlignmentCenter];15[topView setBackgroundColor:[UIColor whiteColor]];16[textLabel setTextColor:[UIColor whiteColor]];17UIImageView* imageView=[[UIImageView alloc] initWithFrame:CGRectMake(5,5,30,34)];18imageView.backgroundColor=[UIColor yellowColor];19[topView addSubview:imageView];20UILabel* desLabel=[[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+5.0f,5.0, topView.bounds.size.width-5*3-imageView.bounds.size.width,34)];21[desLabel setBackgroundColor:[UIColor yellowColor]];22desLabel.text=@"描述性文字水电费水电费水电费时代";23[topView addSubview:desLabel];24#warning适配 (包括横竖屏适配)25[desLabel setAutoresizesSubviews:YES];26[textLabel setAutoresizesSubviews:YES];27[topView setAutoresizesSubviews:YES];28//desLabel 左边距,上边距不变,宽度变化29[desLabel setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleWidth];30//textLabel宽度变化,左,右,上,下边距固定31[textLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin];32//topView宽度变化,左,右,上,下边距固定33[topView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin];34[topView addSubview:textLabel];35//textLabel.hidden=YES;36[self.view addSubview:topView];373839}

View Code

自动布局

masonry自动布局:

适配场景:

1.让xx距离xx左边界40pt

make.left.lessThanOrEqualTo(40);

make.left.equalTo(self.view).with.offset(40);

ios8 size class:

对iPad和iPhone不同尺寸的设备对屏幕进行抽象分类

iPad不管是横屏还是竖直方向都是w:(regular)/h:(regular) 规则/规则

iPhone竖直方向下都是w:(compact)/h:(regular) 紧凑/规则

iPhone横屏方向下是w:(compact)/h:(compact) 紧凑/紧凑

iPhone6 Plus横屏方向下是w:(regular)/h:(compact)规则/紧凑

代码中涉及到对size class的监控:

UIViewController实现UITraitEnvironment协议

storyboard自动布局操作备忘:

添加约束的过程中会看到约束的线是黄颜色,表明当前的约束还不能确定view的frame,需要继续添加,当添加完4个约束后,约束线的颜色是蓝色的,表明当前约束是正确的。

项目在iOS8.x上和新的xcode上使用遇到的问题备忘:

http://segmentfault.com/a/1190000002429129

ios的坐标系

iOS8.x之前的:http://blog.csdn.net/smallmuou/article/details/8238513

ios8之后的坐标系统:window的坐标系会变的,之前的不会变

横竖屏适配关键代码:

1UILabel*lable;2UIView*windowrootView;345//获取当前UIView屏幕方向下的尺寸6+(CGSize)fixedScreenSize {7CGSize screenSize =[UIScreen mainScreen].bounds.size;8if((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) &&UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {9returnCGSizeMake(screenSize.height, screenSize.width);10}else{11returnscreenSize;12}13}1415161718//当前屏幕方向,水平方向为宽度19CGSize size=[BannerViewController fixedScreenSize];20CGRect rect=CGRectMake(0,0, size.width, size.height);21if(NSFoundationVersionNumber <=NSFoundationVersionNumber_iOS_7_1)22{23CGSize temp=[UIScreen mainScreen].bounds.size;24rect.size.width=temp.width;25rect.size.height=temp.height;26}2728windowrootView=[[UIView alloc] initWithFrame:rect];29windowrootView.backgroundColor=[UIColor yellowColor];30[[UIApplication sharedApplication].keyWindow addSubview:windowrootView];313233[lable removeFromSuperview];34lable=[[UILabel alloc] initWithFrame:CGRectMake(20,100,200,50)];35lable.text =@"深圳欢迎您深圳欢迎您";3637[windowrootView addSubview:lable];3839404142//ios7.x之前修改角度43if(NSFoundationVersionNumber <=NSFoundationVersionNumber_iOS_7_1)44{45CGAffineTransform rotate=CGAffineTransformIdentity;46UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];47switch(orientation) {48caseUIInterfaceOrientationLandscapeLeft:49rotate= CGAffineTransformMakeRotation(-M_PI/2);50

break;51caseUIInterfaceOrientationLandscapeRight:52rotate = CGAffineTransformMakeRotation(M_PI/2);53

break;54default:55

break;56}57[lable setTransform:rotate];58}59//iOS8.x之后修改windowsrootView的bounds

View Code

iOS8,iPhone6,iPhone6 plus有一个放大模式,监控到的[UIScreen mainScreen].bounds会有所不一样,特别mark下

参考:http://blog.csdn.net/lvmaker/article/details/43192925

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航