返回列表 發帖

[Unity] iOS 6 後之自動旋轉問題

本帖最後由 loinjoe80 於 2016-4-26 11:27 編輯

在General的設定裡,也設定了Portrait的設定,進入想關閉自動旋轉testViewController是用下列的code
  1. TestViewController * testViewController = [[TestViewController alloc]init];
  2. UINavigationController* navController_ = [[UINavigationController alloc] initWithNavigationBarClass:nil toolbarClass:nil];
  3. navController_.viewControllers = @[testViewController];
  4.    
  5. [UnityGetMainWindow().rootViewController presentViewController:navController_ animated:NO completion:^{ }];
複製代碼
但是testViewController畫面還能自動旋轉,之後利用code的方式關閉自動旋轉,
爬了許多文,大部分的代碼都是以下列的code達成關閉自動旋轉功能
  1. #if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
  2. - (NSUInteger)supportedInterfaceOrientations
  3. #else
  4. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  5. #endif
  6. {
  7.     return UIInterfaceOrientationMaskPortrait;
  8. }
複製代碼
  1. -(BOOL)shouldAutorotate {
  2.     return NO;
  3. }
複製代碼
但是執行後它還是會轉,而且也確認了testViewController的父類別是UIViewController
不知我有哪些細節沒有注意到.
可創建UINavigationController的category,並寫入shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation方法.
代碼如下:
  1. @implementation UINavigationController (Rotation)
  2. -(BOOL)shouldAutorotate
  3. {
  4.     return [[self.viewControllers lastObject] shouldAutorotate];
  5. }

  6. -(NSUInteger)supportedInterfaceOrientations
  7. {
  8.     return [[self.viewControllers lastObject] supportedInterfaceOrientations];
  9. }

  10. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  11. {
  12.     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
  13. }
  14. @end
複製代碼
返回列表