iOS9 supportedInterfaceOrientationsForWindow 停止调用

iOS9 supportedInterfaceOrientationsForWindow stops getting called

我想在横向模式下显示 1 或 2 个 UIViewcontroller,而其他在纵向模式下。 为此,我在 AppDelegate 中实现了这个功能。

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return self.orientation;
}

在 AppDelegate.h 中,方向是:

@property (nonatomic, assign) UIInterfaceOrientationMask orientation;

在我需要横向方向的 UIViewcontroller 中。我把这个代码

-(void)viewWillAppear:(BOOL)animated
{
    self.appDelegate.orientation = UIInterfaceOrientationMaskLandscape;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.appDelegate.orientation = UIInterfaceOrientationMaskPortrait;
}

然而,当我去 'LandscapeViewController' 时它工作正常,我回去,它工作正常,我再次去 'LandscapeViewController' 它正常,然后当我回来时,supportedInterfaceOrientationsForWindow 方法停止被调用。有什么理由吗?或者我在做一些奇怪的事情?

如果您使用的是 UITabBarController,请为 UITabBarController 创建自定义 class 并将此代码放在那里

-(UIInterfaceOrientationMask) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

并根据您的需要将其放置在您的视图控制器中。

将此代码放入 appDelegate

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

    id rootViewController = [self topViewControllerWithRootViewController:window.rootViewController];

    if (rootViewController != nil)
    {
        if ([rootViewController respondsToSelector:@selector(canRotate)])
        {
            return UIInterfaceOrientationMaskLandscape;
        }
    }
    return UIInterfaceOrientationMaskPortrait;
}

-(UIViewController*) topViewControllerWithRootViewController:(id)rootViewController
{

    if (rootViewController == nil)
    {
        return nil;
    }

    if ([rootViewController isKindOfClass:[UITabBarController  class]])
    {
        UITabBarController *selectedTabBarController = rootViewController;
        return [self topViewControllerWithRootViewController:selectedTabBarController.selectedViewController];
    }
    else if ([rootViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *selectedNavController = rootViewController;
        return [self topViewControllerWithRootViewController:selectedNavController.visibleViewController];
    }
    else
    {
        UIViewController *selectedViewController = rootViewController;
        if (selectedViewController.presentedViewController != nil)
        {
            return [self topViewControllerWithRootViewController:selectedViewController.presentedViewController];
        }
    }
    return rootViewController;
}

并将其放入您的视图控制器

-(void)canRotate
{

}