关闭显示的视图控制器时强制使用肖像模式

use*_*281 5 uiviewcontroller device-orientation ios8

我有一个介绍的视图控制器,它支持所有界面方向。但是,当前视图控制器仅应支持纵向模式。

到目前为止,一切都很好。

但是,在iOS8中,当我以横向模式关闭视图控制器WHILE时,横向模式保持不变。而且由于我已经shouldAutorotate设置了NO它,所以永不回头。

问题是,如何强制演示的VC返回肖像?

我目前已实现此替代方法:

- (BOOL)shouldAutorotate
{
  if ([self interfaceOrientation] != UIInterfaceOrientationPortrait)
  {
    return YES;
  }
  else
  {
    return NO;
  } 
}
Run Code Online (Sandbox Code Playgroud)

它允许将设备移动到纵向模式,并且它将留在此处,因为在纵向模式后,自动旋转被禁用了。

但这看起来很难看,直到用户旋转手机为止。

怎么强迫呢?

Mus*_*sis 0

此代码将强制 UI 返回纵向,假设您尝试强制纵向的视图控制器已经是根视图控制器(如果它还不是根,此代码将恢复根视图控制器,但不会恢复任何其他视图控制器)查看已推送到其上的控制器):

UIInterfaceOrientation orientation = [[UIApplication 
    sharedApplication] statusBarOrientation];

if (orientation != UIInterfaceOrientationPortrait) {

    // HACK: setting the root view controller to nil and back again "resets" 
    // the navigation bar to the correct orientation
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIViewController *vc = window.rootViewController;
    window.rootViewController = nil;
    window.rootViewController = vc;

}
Run Code Online (Sandbox Code Playgroud)

它不是很漂亮,因为它在顶级视图控制器被关闭后突然跳转,但它比将其留在横向更好。