ios橫豎屏的效果是不相同的,其效果也是不一樣的。是以我們在開發中如果允許螢幕橫豎屏間的切換,那麼我們就要調整視圖的布局。利用Interface Builder開發,我們可以快速的拖拽出合适的界面布局,但是螢幕自動切換布局不能很好的适配,下圖是,沒有做任何調整的狀态下,實作的橫豎屏切換,可以看到界面不是很美觀。

目前我所知的實作ios橫豎屏切換的解決方案共有三種:
- 利用Interface Builder擴充卡自動适配調整界面。
- 在橫豎屏切換時,每個控件重新布局。
- 利用Interface Builder建立兩個視圖,橫屏時切換到橫屏視圖,豎屏時切換到豎屏視圖。
在ios中,橫豎屏切換時,會調用下面函數:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
//zuo
}
if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
//you
}
if (interfaceOrientation==UIInterfaceOrientationPortrait) {
//shang
}
if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//xia
}
return YES;
}
傳回yes表示切換螢幕,傳回no是不能向相應的方向切換視圖。
下面分别介紹一下三種方法,第一種方法最簡單,但是效果是最差的,我們隻需用Interface bulider修改相應的屬性即可。實作的效果如下:
實作的方法:
選中控件,按command+3,上圖紅框部分的紅線表示距離不能自動适配,要是虛線表示距離可以自動适配。我們選擇可以自動适配,最後的結果就如上圖。
第二中方法:
第二中方法是相應的控件和代碼相關聯:代碼:
@interface ipad_demooViewController : UIViewController {
IBOutlet UIButton *myButton1;
IBOutlet UIButton *myButton2;
IBOutlet UIButton *myButton3;
IBOutlet UIButton *myButton4;
IBOutlet UIButton *myButton5;
IBOutlet UIButton *myButton6;
}
@property (nonatomic,retain) UIButton *myButton1;
@property (nonatomic,retain) UIButton *myButton2;
@property (nonatomic,retain) UIButton *myButton3;
@property (nonatomic,retain) UIButton *myButton4;
@property (nonatomic,retain) UIButton *myButton5;
@property (nonatomic,retain) UIButton *myButton6;
@end
和IB相關聯:
更改每一個控件的布局:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
//zuo
self.myButton1.frame=CGRectMake(86, 208, 72, 37);
self.myButton2.frame=CGRectMake(480, 208, 72, 37);
self.myButton3.frame=CGRectMake(86, 308, 72, 37);
self.myButton4.frame=CGRectMake(480, 308, 72, 37);
self.myButton5.frame=CGRectMake(86, 408, 72, 37);
self.myButton6.frame=CGRectMake(480, 408, 72, 37);
}
if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
//you
self.myButton1.frame=CGRectMake(86, 208, 72, 37);
self.myButton2.frame=CGRectMake(480, 208, 72, 37);
self.myButton3.frame=CGRectMake(86, 308, 72, 37);
self.myButton4.frame=CGRectMake(480, 308, 72, 37);
self.myButton5.frame=CGRectMake(86, 408, 72, 37);
self.myButton6.frame=CGRectMake(480, 408, 72, 37);
}
if (interfaceOrientation==UIInterfaceOrientationPortrait) {
//shang
self.myButton1.frame=CGRectMake(86, 208, 72, 37);
self.myButton2.frame=CGRectMake(480, 208, 72, 37);
self.myButton3.frame=CGRectMake(86, 308, 72, 37);
self.myButton4.frame=CGRectMake(480, 308, 72, 37);
self.myButton5.frame=CGRectMake(86, 408, 72, 37);
self.myButton6.frame=CGRectMake(480, 408, 72, 37);
}
if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//xia
self.myButton1.frame=CGRectMake(86, 208, 72, 37);
self.myButton2.frame=CGRectMake(480, 208, 72, 37);
self.myButton3.frame=CGRectMake(86, 308, 72, 37);
self.myButton4.frame=CGRectMake(480, 308, 72, 37);
self.myButton5.frame=CGRectMake(86, 408, 72, 37);
self.myButton6.frame=CGRectMake(480, 408, 72, 37);
}
return YES;
}
第三種方法是建立兩個視圖,下面看一下實作過程:
首先建立兩個視圖:
IBOutlet UIView *hView;
IBOutlet UIView *vView;
建立相應的@property方法.
然後在IB中在複制一個view。
把一個視圖做橫屏時的布局,一個view做豎屏時的布局。把相應的view和相應的方法相連接配接,在設定一個預設視圖為view。
下面就是代碼實作:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
//zuo
self.view=self.hView;
}
if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
//you
self.view=self.hView;
}
if (interfaceOrientation==UIInterfaceOrientationPortrait) {
//shang
self.view=self.vView;
}
if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//xia
self.view=self.vView;
}
return YES;
}
實作的效果如下:
上述就是我目前知道的三種橫豎屏解決方案,我們可以看到第三種比較簡單,但是編寫比較麻煩,實作複雜邏輯比較麻煩,第二種方法實作起來不直覺,調試比較麻煩,但是效果最好。
源代碼:http://easymorse-iphone.googlecode.com/svn/trunk/ipad.demoo/