preferredInterfaceOrientationForPresentation未被调用

时间:2013-08-20 08:18:01

标签: ios objective-c uiinterfaceorientation

我有一个场景,其中我有一个带有5个标签的UITabbarController。每个选项卡都包含一个UINavigationController。

现在在其中一个UINavigationController rootViewController中,当我选择一个选项时,会推送另一个viewcontroller。现在我希望另一个视图控制器只是Landscape。

以下是我的UITabBarController n代码的UINavigationController类的代码

@implementation UITabBarController (rotation)

-(BOOL)shouldAutorotate
{
    if ([self.selectedViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.selectedViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    else if(DEVICE_IS_IPAD)
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.selectedViewController preferredInterfaceOrientationForPresentation];
    }
    return 0;
}

@end

@implementation UINavigationController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
    if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.topViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

-(NSUInteger) supportedInterfaceOrientations {
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if ([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.topViewController preferredInterfaceOrientationForPresentation];
    }
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationPortraitUpsideDown;
}
@end

我想成为Landscape的ViewController的代码是:

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    if(DEVICE_IS_IPAD)
        return UIInterfaceOrientationMaskLandscape;
    else
        return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

主要问题是preferredInterfaceOrientationForPresentation根本没有调用UITabbarController或UINavigationBarController,甚至我想要显示的视图控制器。

你能告诉我我做错了吗?

感谢。

2 个答案:

答案 0 :(得分:1)

您可能忘了将supportedInterfaceOrientationsForWindow添加到AppDelegate?

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return  UIInterfaceOrientationMaskAll;
}

答案 1 :(得分:0)

小心零。我正在遵循类似的子类化UINavigationController的方法来将旋转消息中继到topViewController。除了应用程序启动时,它工作得很好。当你启动应用程序时,没有topViewController(但是),但操作系统需要确定支持的方向NOW和最顶层的viewController(UINavigationController)。如果您未能在合适的时间提供答案,UINavigationController将以错误的方向出现。

注意:UINavigationController不会调用PreferredInterfaceOrientationForPresentation

  

如果您使用UINavigationController作为根窗口   控制器,它将是它的应用程序&   supportedInterfaceOrientations将被调用。   iOS6: supportedInterfaceOrientations not working (is invoked but the interface still rotates)