1

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

现在在其中一个 UINavigationController rootViewController 中,当我选择一个选项时,会推送另一个视图控制器。现在我希望另一个视图控制器仅是 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

我只想成为景观的 ViewController 的代码是:

-(BOOL)shouldAutorotate
{
    return YES;
}

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

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

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

你能告诉我我做错了什么吗?

谢谢。

4

2 回答 2

1

可能是您忘记添加supportedInterfaceOrientationsForWindow到您的 AppDelegate 了吗?

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return  UIInterfaceOrientationMaskAll;
}
于 2013-08-20T08:25:04.533 回答
0

谨防零。我正在遵循一种类似的子类化 UINavigationController 的方法,将旋转消息传递给 topViewController。它工作得很好,除了应用程序启动的时间。当您启动应用程序时(还没有)topViewController,但操作系统现在需要确定支持的方向以及最顶层的 viewController(UINavigationController)。如果您未能在正确的时间提供答案,UINavigationController 将出现在错误的方向。

注意:不为 UINavigationController 调用 PreferredInterfaceOrientationForPresentation

如果您使用 UINavigationController 作为根窗口控制器,它将调用它的 shouldAutorotate 和 supportedInterfaceOrientations。 iOS6:supportedInterfaceOrientations 不起作用(被调用但界面仍然旋转)

于 2017-02-23T02:54:54.463 回答