2016-04-07 7 views
0

ViewControllerA представляет ViewControllerB modally.iOS - Принудительная ориентация устройства, если контроллер вида представлен модально

ViewControllerA должен быть портретной ориентации.

ViewControllerB должен быть ориентирован на ландшафт.

я: ViewControllerA:

override func shouldAutorotate() -> Bool { 
     return false 
    } 
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
     return UIInterfaceOrientationMask.Portrait 
    } 
    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation { 
     return UIInterfaceOrientation.Portrait 
    } 

ViewControllerB

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation { 
     return UIInterfaceOrientation.LandscapeRight 
    } 

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
     return UIInterfaceOrientationMask.LandscapeRight 
    } 

работах, но когда я закрываю ViewControllerB, ViewControllerA становится также LandscapeRight. Как это исправить? Thx.

РЕШЕНИЕ:

Редактировать AppDelegete файл.

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { 

     if self.window?.rootViewController?.presentedViewController is ViewControllerB { 

      let secondController = self.window!.rootViewController!.presentedViewController as! ViewControllerB 

      if secondController.isPresented { 
       return UIInterfaceOrientationMask.LandscapeRight; 
      } else { 
       return UIInterfaceOrientationMask.Portrait; 
      } 
     } else { 
      return UIInterfaceOrientationMask.Portrait; 
     } 
    } 
+0

Ваше решение является правильным, но он будет получать очень сложно, если вы будете иметь больше ViewControllers в разных направлениях. Вы можете попробовать мое решение, оно работает в моем сценарии. – KlimczakM

ответ

1

Вы должны разрешить авторотации в ViewControllerA, потому что он должен вернуться в портретный режим после ViewControllerB увольнения. В дальнейшем он не будет авторотировать, потому что вы разрешаете только портретную ориентацию на этом экране.

Решение:

override func shouldAutorotate() -> Bool { 
    return true 
} 
+0

это не работает для меня – phnmnn

+0

@phnmnn Вы используете 'UINavigationController' между ними? Или это просто 'UIViewController'? – KlimczakM

+0

UINavigationController – phnmnn

Смежные вопросы