【问题标题】:iOS9 supportedInterfaceOrientationsForWindow stops getting callediOS9 supportedInterfaceOrientations For Window 停止被调用
【发布时间】:2017-01-13 02:17:57
【问题描述】:

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

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

在 AppDelegate.h 中,方向是:

@property (nonatomic, assign) UIInterfaceOrientationMask orientation;

在我需要横向方向的 UIViewcontroller(s) 中。我把这段代码

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

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

但是,当我转到“LandscapeViewController”时,它工作正常,我返回,它工作正常,我再次转到“LandscapeViewController”,它正常,然后当我返回时,supportedInterfaceOrientationsForWindow 方法停止被调用。有什么理由吗?还是我在做一些奇怪的事情?

【问题讨论】:

    标签: ios objective-c orientation uiinterfaceorientation


    【解决方案1】:

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

    -(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
    {
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-05
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多