2016-11-25 10 views
0

Я работаю над проектом, где я хочу заменить значки на основе ориентации устройства, не позволяя вращаться viewController. У меня есть несколько viewController, которым разрешено вращать и другие, указанные в моем контроллере навигации, которым не разрешено вращаться.Как определить ориентацию устройства, когда shouldAutorotate = NO и разрешен только портрет?

Эта реализация работает именно так, как я ее хочу.

код в моей навигации контроллера:

- (BOOL)shouldAutorotate 
{ 
    id currentViewController = self.topViewController; 

    if ([currentViewController isKindOfClass:[ChimpTabBarController class]]) { 
     ChimpTabBarController *tabbarController = (ChimpTabBarController*)currentViewController; 

     if (tabbarController.selectedIndex == 0){ 
      return NO; 
     } 
    } else if ([currentViewController isKindOfClass:[RotationViewController class]] || [currentViewController isKindOfClass:[ChimpBlackoutVC class]] || [currentViewController isKindOfClass:[EditPictureVC class]]) { 
     return NO; 
    } 
    return YES; 
} 



-(UIInterfaceOrientationMask)supportedInterfaceOrientations{ 
    id currentViewController = self.topViewController; 

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 


    if ([currentViewController isKindOfClass:[ChimpTabBarController class]]) { 
     ChimpTabBarController *tabbarController = (ChimpTabBarController*)currentViewController; 

     if (tabbarController.selectedIndex == 0){ 
      return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown); 
     } 
    } else if ([currentViewController isKindOfClass:[RotationViewController class]] || [currentViewController isKindOfClass:[ChimpBlackoutVC class]] || [currentViewController isKindOfClass:[EditPictureVC class]]) { 
     return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown); 
    } 
    return UIInterfaceOrientationMaskAll; 
} 

Есть ли способ, чтобы выяснить, какие вращение мое устройство хочет идти, но не допускается? Так что я могу изменить направление стрелки, слова и т.д. ...

Благодаря

ответ

0

Я нашел способ сам, скорее раньше, чем ожидалось.

Для вашего приложения вы можете использовать:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 

Но когда вращение отключено вы всегда можете задать ориентацию устройства:

[[UIDevice currentDevice] orientation] 

Затем в ViewController вы добавляете наблюдателя, когда устройство изменило ориентацию:

->How to check Device Orientation Change From Portrait To Landscape and Vice-Versa in iPad

Надеюсь, это поможет людям в будущем.