iOS:状态栏和键盘在应用程序后台旋转

时间:2016-12-19 16:00:43

标签: ios10 uiinterfaceorientation

我有一个只有99%肖像的应用程序,只有一个viewcontroller处于横向状态。因此,我的Info.plist包含三种可能的方向:纵向和横向。

一切正常,但是:当我将我的应用程序带到背景和前面时,状态栏(以及,如果显示,键盘)在旋转设备时旋转到横向。我的观点仍然正确。

这是一个错误还是我错过了什么?

1 个答案:

答案 0 :(得分:2)

您可以尝试在App Delegate中实施application:supportedInterfaceOrientationsForWindow:

来自Apple文档:

  

此方法返回应用程序支持的总界面方向集。在确定是否旋转特定视图控制器时,此方法返回的方向与根视图控制器或最顶层呈现的视图控制器支持的方向相交。应用程序和视图控制器必须在允许轮换之前达成一致。   如果您未实现此方法,则应用程序会使用应用程序Info.plist的UIInterfaceOrientation键中的值作为默认界面方向。

然后你可以添加这样的东西(假设你的一个横向控制器是MyLandscapeOnlyViewController):

<强>目标C

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    // Return the desired orientation for your custom-oriented ViewController 
    if ([window.rootViewController.presentedViewController isKindOfClass:[MyLandscapeOnlyViewController class]])
    {
        return UIInterfaceOrientationMaskLandscape;
    }

    // Default orientation is portrait
    return UIInterfaceOrientationMaskPortrait;
}

Swift 3

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    // Return the desired orientation for your custom-oriented ViewController
    if (window?.rootViewController?.presentedViewController?.isKind(of: MyLandscapeOnlyViewController.self) == true) {
        return .landscape
    }

    // Default orientation is portrait
    return .portrait
}

根据您的视图层次结构,对最顶层呈现的ViewController的检查可能更具挑战性,但这是一般的想法。

希望这有帮助!