iOS橫豎屏解決方案

來源:互聯網
上載者:User

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

目前我所知的實現ios橫豎屏切換的解決方案共有三種:

1.利用Interface Builder適配器自動適配調整介面。

2.在橫豎屏切換時,每個控制項重新布局。

3.利用Interface Builder建立兩個視圖,橫屏時切換到橫屏視圖,豎屏時切換到豎屏視圖。

在ios中,橫豎屏切換時,會調用下面函數:

 
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
  2.         if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) { 
  3.             //zuo 
  4.         } 
  5.         if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) { 
  6.             //you 
  7.         } 
  8.         if (interfaceOrientation==UIInterfaceOrientationPortrait) { 
  9.             //shang 
  10.         } 
  11.         if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) { 
  12.             //xia 
  13.         } 
  14.         return YES; 
  15.     } 
  16.   

返回yes表示切換畫面,返回no是不能向相應的方向切換視圖。

下面分別介紹一下三種方法,第一種方法最簡單,但是效果是最差的,我們只需用Interface bulider修改相應的屬性即可。實現的效果如下:

實現的方法:

選中控制項,按command+3,紅框部分的紅線表示距離不能自動適配,要是虛線表示距離可以自動適配。我們選擇可以自動適配,最後的結果就如。

第二種方法:

第二種方法是相應的控制項和代碼相關聯:

代碼:

 
  1. @interface ipad_demooViewController : UIViewController { 
  2.  
  3.        IBOutlet UIButton *myButton1; 
  4.         IBOutlet UIButton *myButton2; 
  5.         IBOutlet UIButton *myButton3; 
  6.         IBOutlet UIButton *myButton4; 
  7.         IBOutlet UIButton *myButton5; 
  8.         IBOutlet UIButton *myButton6; 
  9.     } 
  10.     @property (nonatomic,retain) UIButton *myButton1; 
  11.     @property (nonatomic,retain) UIButton *myButton2; 
  12.     @property (nonatomic,retain) UIButton *myButton3; 
  13.     @property (nonatomic,retain) UIButton *myButton4; 
  14.     @property (nonatomic,retain) UIButton *myButton5; 
  15.     @property (nonatomic,retain) UIButton *myButton6; 
  16.  
  17.     @end 
  18.   

和IB相關聯:

更改每一個控制項的布局:

 
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
  2.        if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) { 
  3.            //zuo 
  4.            self.myButton1.frame=CGRectMake(86, 208, 72, 37); 
  5.            self.myButton2.frame=CGRectMake(480, 208, 72, 37); 
  6.            self.myButton3.frame=CGRectMake(86, 308, 72, 37); 
  7.            self.myButton4.frame=CGRectMake(480, 308, 72, 37); 
  8.            self.myButton5.frame=CGRectMake(86, 408, 72, 37); 
  9.            self.myButton6.frame=CGRectMake(480, 408, 72, 37); 
  10.        } 
  11.        if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) { 
  12.            //you 
  13.            self.myButton1.frame=CGRectMake(86, 208, 72, 37); 
  14.            self.myButton2.frame=CGRectMake(480, 208, 72, 37); 
  15.            self.myButton3.frame=CGRectMake(86, 308, 72, 37); 
  16.            self.myButton4.frame=CGRectMake(480, 308, 72, 37); 
  17.            self.myButton5.frame=CGRectMake(86, 408, 72, 37); 
  18.            self.myButton6.frame=CGRectMake(480, 408, 72, 37); 
  19.        } 
  20.        if (interfaceOrientation==UIInterfaceOrientationPortrait) { 
  21.            //shang 
  22.            self.myButton1.frame=CGRectMake(86, 208, 72, 37); 
  23.            self.myButton2.frame=CGRectMake(480, 208, 72, 37); 
  24.            self.myButton3.frame=CGRectMake(86, 308, 72, 37); 
  25.            self.myButton4.frame=CGRectMake(480, 308, 72, 37); 
  26.            self.myButton5.frame=CGRectMake(86, 408, 72, 37); 
  27.            self.myButton6.frame=CGRectMake(480, 408, 72, 37); 
  28.        } 
  29.        if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) { 
  30.            //xia 
  31.            self.myButton1.frame=CGRectMake(86, 208, 72, 37); 
  32.            self.myButton2.frame=CGRectMake(480, 208, 72, 37); 
  33.            self.myButton3.frame=CGRectMake(86, 308, 72, 37); 
  34.            self.myButton4.frame=CGRectMake(480, 308, 72, 37); 
  35.            self.myButton5.frame=CGRectMake(86, 408, 72, 37); 
  36.            self.myButton6.frame=CGRectMake(480, 408, 72, 37); 
  37.        } 
  38.        return YES; 
  39.    } 

第三種方法是建立兩個視圖,下面看一下實現過程:

首先建立兩個視圖:

 
  1. IBOutlet UIView *hView; 
  2. IBOutlet UIView *vView; 
  3.   

建立相應的@property方法.

然後在IB中在複製一個view。

把一個視圖做橫屏時的布局,一個view做豎屏時的布局。把相應的view和相應的方法相串連,在設定一個預設視圖為view。

下面就是代碼實現:

 
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
  2.         if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) { 
  3.             //zuo 
  4.             self.view=self.hView; 
  5.            } 
  6.         if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) { 
  7.             //you 
  8.             self.view=self.hView; 
  9.           } 
  10.         if (interfaceOrientation==UIInterfaceOrientationPortrait) { 
  11.             //shang 
  12.             self.view=self.vView; 
  13.            } 
  14.         if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) { 
  15.             //xia 
  16.             self.view=self.vView; 
  17.           } 
  18.         return YES; 
  19.     } 
  20.   

實現的效果如下:

上述就是我目前知道的三種橫豎屏解決方案,我們可以看到第三種比較簡單,但是編寫比較麻煩,實現複雜邏輯比較麻煩,第二種方法實現起來不直觀,調試比較麻煩,但是效果最好。

原始碼:http://easymorse-iphone.googlecode.com/svn/trunk/ipad.demoo/

ios6.0橫豎屏切換問題解決

this class is not key value coding-compliant for the key

ios5裡面的旋轉方法ios6裡面確實掉不到了,但是還是可以用的。

首先,在app的主介面也就是自己的主ViewController.m)裡面加上

 
  1. -(NSUInteger)supportedInterfaceOrientations{ 
  2.     return UIInterfaceOrientationMaskAllButUpsideDown;//這裡返回哪個值,就看你想支援那幾個方向了。這裡必須和後面plist檔案裡面的一致我感覺是這樣的)。 
  3.  
  4. - (BOOL)shouldAutorotate { 
  5.     return YES;//支援轉屏 
  6.   

這兩個函數。

然後在plist檔案裡面找到Supported interface orientations (iPad)選項,添加你想支援的方向,都有提示的。

然後問題就解決了。

也許我描述的還有問題,希望你能指正。謝謝了。

 
  1. -(NSUInteger)supportedInterfaceOrientations{ 
  2.     return UIInterfaceOrientationMaskAllButUpsideDown;//這裡返回哪個值,就看你想支援那幾個方向了。這裡必須和後面plist檔案裡面的一致我感覺是這樣的)。 
  3.   

這裡的設定會覆蓋掉plist中的值

還有需要注意:mainViewController要設定為window的rootViewController,addSubView上去可能存在問題。並且上面的所有subViewController都會受到rootViewController支援朝向的影響

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.