iOS 實現單個頁面支援橫豎屏,其他頁面只能豎屏

來源:互聯網
上載者:User

標籤:turn   assign   技術   nbsp   測試   ==   一個   pre   cto   

 最近在自己的項目裡面  有需要做一個需求 : app中某一個頁面支援橫豎屏, 而其他頁面只能豎屏。 
  • 1
  • 2

實現方法如下: 
1 首先需要Xcode中選中支援的螢幕方向 

2 Appdelegate中 
.h

@property (nonatomic,assign)NSInteger allowRotate; 
  • 1

.m中

//此方法會在裝置橫豎屏變化的時候調用- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ //   NSLog(@"方向  =============   %ld", _allowRotate);    if (_allowRotate == 1) {        return UIInterfaceOrientationMaskAll;    }else{        return (UIInterfaceOrientationMaskPortrait);    }}// 返回是否支援裝置自動旋轉- (BOOL)shouldAutorotate{    if (_allowRotate == 1) {        return YES;    }    return NO;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3 在需要支援橫豎屏的controller中:

viewWillApplear 中

 //在視圖出現的時候,將allowRotate改為1,    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;    delegate.allowRotate = 1;
  • 1
  • 2
  • 3

viewWillDisappear中

 //在視圖出現的時候,將allowRotate改為0,    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;    delegate.allowRotate = 0;
  • 1
  • 2
  • 3

寫好以上代碼之後, 會發現一些問題: 當橫屏頁面直接點擊“返回”按鈕退出的時候, 頁面依然是橫屏, 而我們需要的是僅一個頁面可以橫屏,測試需要在viewWillDisappear中加入如下代碼:

    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {        SEL selector = NSSelectorFromString(@"setOrientation:");        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];        [invocation setSelector:selector];        [invocation setTarget:[UIDevice currentDevice]];        int val = UIInterfaceOrientationPortrait;        [invocation setArgument:&val atIndex:2];        [invocation invoke];    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

此時就可以使app僅有設定頁面支援橫豎屏了!

此時如果app要求使用者在橫屏 豎屏的模式下改變UI(橫屏與豎屏對應不同的UI), 可以在以下方法中執行

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{    // do something before rotation    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {         螢幕從豎屏變為橫屏時執行    }else{        螢幕從橫屏變為豎屏時執行    }  }- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{    // do something after rotation}

iOS 實現單個頁面支援橫豎屏,其他頁面只能豎屏

相關文章

聯繫我們

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