詳解iPad橫豎屏切換解決方案

來源:互聯網
上載者:User

詳解iPad橫豎屏切換解決方案是本文要介紹的內容,不多說,先來看內容。由於ipad橫豎屏不同,所以好的應用,橫豎屏的頁面配置也不一樣。那麼就需要橫豎屏的整體解決方案。先看一個橫豎屏布局不一樣的介面。

 

上面兩張圖是來自同一個介面的橫豎版的截屏。可以看出,橫豎版顯示的內容相同,但是介面布局不同。要實現上述布局,主要是運用UIView中layoutSubviews方法。當UIView設定為自動適配螢幕時,當使用者旋轉裝置的時候,會調用layoutSubviews方法,我們只需重寫這個方法,然後判斷使用者螢幕的方向。在調整每個空間的位置即可。

下面是實現上述介面的最簡單的原型:

首先分析可以知道左面是圖片,右面是一個圖片加文字的視圖。下面就實現一個左面視圖右面是一個圖加一段字的案例。

案例的如下:

 

其中右面的文字和綠色部分是用一個子視圖封裝的。

整個布局是我在主視圖中添加了一個ContentView視圖,在ContentView視圖中添加了一個ArticleView視圖。

其中ArticleView和ContentView的xib檔案都開啟了

在ContentView中重寫layoutSubviews方法,然後根據stausbar的方向判斷當前視圖的橫豎屏。具體代碼:

 
  1. -(void)layoutSubviews{   
  2.     [super layoutSubviews];   
  3.     UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation];   
  4.     if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {   
  5.         //翻轉為豎屏時   
  6.         [self setVerticalFrame];   
  7.     }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) {   
  8.         //翻轉為橫屏時   
  9.         [self setHorizontalFrame];   
  10.     }   
  11. }   
  12.  
  13. -(void)setVerticalFrame   
  14. {   
  15.     NSLog(@"豎屏");   
  16.     [titleLable setFrame:CGRectMake(283, 0, 239, 83)];   
  17.     [leftView setFrame:CGRectMake(38, 102, 384, 272)];   
  18.     [rightView setFrame:CGRectMake(450, 102, 282, 198)];   
  19. }   
  20.  
  21. -(void)setHorizontalFrame   
  22. {   
  23.     NSLog(@"橫屏");   
  24.     [titleLable setFrame:CGRectMake(183, 0, 239, 83)];   
  25.     [leftView setFrame:CGRectMake(168, 122, 384, 272)];   
  26.     [rightView setFrame:CGRectMake(650, 122, 282, 198)];   

在具體的橫豎屏方法中,從新設定各個組件的座標即可。

接下來在ContentView中添加ArticleView視圖。

 
  1. -(id)initWithCoder:(NSCoder *)aDecoder   
  2. {   
  3.     if ((self = [super initWithCoder:aDecoder])) {   
  4.  
  5.         NSArray *arrayContentView =[[NSBundle mainBundle] loadNibNamed:@"ArticleView" owner:self options:nil];   
  6.         rightView=[arrayContentView objectAtIndex:0];   
  7.         [self addSubview:rightView];   
  8.     }   
  9.     return self;   

由於我用的是xib,所以初始化方法為initWithCoder,在這個中添加新的視圖。

同樣在ArticleView中設定橫豎屏相應空間的座標即可。

 
  1. -(void)layoutSubviews{   
  2.     [super layoutSubviews];   
  3.     UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation];   
  4.     CGRect rect=self.frame;   
  5.     rect.size.width=282;   
  6.     rect.size.height=198;   
  7.     [self setFrame:rect];   
  8.     if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {   
  9.         //翻轉為豎屏時   
  10.         [self setVerticalFrame];   
  11.     }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) {   
  12.         //翻轉為橫屏時   
  13.         [self setHorizontalFrame];   
  14.     }   
  15. }   
  16.  
  17. -(void)setVerticalFrame   
  18. {   
  19.     NSLog(@"豎屏");   
  20.     [contentView setFrame:CGRectMake(12, 6, 250, 125)];   
  21.     [textLable setFrame:CGRectMake(50, 139, 182, 39)];   
  22. }   
  23.  
  24. -(void)setHorizontalFrame   
  25. {   
  26.     NSLog(@"橫屏");   
  27.     [contentView setFrame:CGRectMake(12, 6, 106, 158)];   
  28.     [textLable setFrame:CGRectMake(135, 11, 147, 39)];   

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

小結:詳解iPad橫豎屏切換解決方案的內容介紹完了,通過ipad螢幕切換的內容,是不是以及很清楚了,最後希望本文對你有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.