
代码固定坐标方案
今天花了一天的时间对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.在每个视图控制器下加入以下代码:
1 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
2 self.edgesForExtendedLayout = UIRectEdgeNone;
3 }
View Code
这样不管在IOS6还是IOS7下,元素的Origin.y都是一样的,不用再去分别设置了
然后只要控制每个元素的高度就行了,如下图:

3.在第二种方法前提下,在每个视图控制器下重写loadView方法
1 -(void) loadView
2 {
3 [super loadView];
4 CGRect rect= [[UIScreen mainScreen] applicationFrame];
5 UIView* view=[[UIView alloc] initWithFrame:rect];
6 view.backgroundColor=[UIColor purpleColor];//根据项目需求,修改你自己的背景颜色
7 self.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) testAutoresizing
2 {
3 //左边距20,上边距64,高度44, 位置水平居中
4 UIView* topView=[[UIView alloc] initWithFrame:CGRectMake(20, 64, [UIScreen mainScreen].bounds.size.width - 20 * 2, 44)];
5 topView.layer.borderWidth=1.0;
6 topView.layer.borderColor=[UIColor blackColor].CGColor;
7 CGFloat textLabelTop=(topView.frame.size.width-200)/2;
8 CGFloat textLabelWidth=(topView.frame.size.height-50)/2;
9 //居中
10 UILabel* 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]];
17 UIImageView* imageView=[[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 30, 34)];
18 imageView.backgroundColor=[UIColor yellowColor];
19 [topView addSubview:imageView];
20 UILabel* 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]];
22 desLabel.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];
37
38
39 }
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的坐标系会变的,之前的不会变
横竖屏适配关键代码:
1 UILabel* lable;
2 UIView* windowrootView;
3
4
5 //获取当前UIView屏幕方向下的尺寸
6 + (CGSize)fixedScreenSize {
7 CGSize screenSize = [UIScreen mainScreen].bounds.size;
8 if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
9 return CGSizeMake(screenSize.height, screenSize.width);
10 } else {
11 return screenSize;
12 }
13 }
14
15
16
17
18 //当前屏幕方向,水平方向为宽度
19 CGSize size= [BannerViewController fixedScreenSize];
20 CGRect rect=CGRectMake(0, 0, size.width, size.height);
21 if(NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)
22 {
23 CGSize temp= [UIScreen mainScreen].bounds.size;
24 rect.size.width=temp.width;
25 rect.size.height=temp.height;
26 }
27
28 windowrootView=[[UIView alloc] initWithFrame:rect];
29 windowrootView.backgroundColor=[UIColor yellowColor];
30 [[UIApplication sharedApplication].keyWindow addSubview:windowrootView];
31
32
33 [lable removeFromSuperview];
34 lable=[[UILabel alloc] initWithFrame:CGRectMake(20, 100, 200, 50)];
35 lable.text = @"深圳欢迎您深圳欢迎您";
36
37 [windowrootView addSubview:lable];
38
39
40
41
42 //ios7.x之前修改角度
43 if(NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1)
44 {
45 CGAffineTransform rotate=CGAffineTransformIdentity;
46 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
47 switch (orientation) {
48 case UIInterfaceOrientationLandscapeLeft:
49 rotate= CGAffineTransformMakeRotation(-M_PI/2);
50 break;
51 case UIInterfaceOrientationLandscapeRight:
52 rotate = CGAffineTransformMakeRotation(M_PI/2);
53 break;
54 default:
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