iOS9 supportedInterfaceOrientationsForWindow停止调用

时间:2016-09-05 17:19:26

标签: ios objective-c orientation uiinterfaceorientation

我希望在横向模式下显示1或2个UIView控制器,而在Portrait中显示其他人。 为此,我在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方法停止被调用。有什么理由吗?或者我在做一些奇怪的事情?

1 个答案:

答案 0 :(得分:4)

如果您正在使用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
{

}