代碼固定坐標方案
今天花了一天的時間對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 }
這樣不管在IOS6還是IOS7下,元素的Origin.y都是一樣的,不用再去分別設置了
然后只要控制每個元素的高度就行了,如下圖:

3.在第二種方法前提下,在每個視圖控制器下重寫loadView方法

1 -(void) loadView2 {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 }
這樣第二種方法里的高度設置都可以設置一樣的,不用分別設置了~~
高度以屏幕高度減去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 }
自動布局
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
iOS8,iPhone6,iPhone6 plus有一個放大模式,監控到的[UIScreen mainScreen].bounds會有所不一樣,特別mark下
參考:http://blog.csdn.net/lvmaker/article/details/43192925
新聞熱點
疑難解答