【问题标题】:IO6 doesn't call -(BOOL)shouldAutorotateIO6 不调用 -(BOOL)shouldAutorotate
【发布时间】:2012-10-11 08:51:42
【问题描述】:

我的应用中有一些我不想支持方向的视图。 在didFinishLaunchingWithOptions 我添加导航:

...
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:self.viewController];

    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
...

在每个ViewController 中我都有UITabBar(我不知道这是否重要)。

在我添加的第一个视图控制器中:

-(BOOL)shouldAutorotate {
        return NO;
    }

    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }

supportedInterfaceOrientations 在视图加载时被调用,但shouldAutorotate 在我旋转设备时不会调用。
我在这里错过了什么?

【问题讨论】:

标签: iphone objective-c ios ios6 uiinterfaceorientation


【解决方案1】:

AppDelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6
{

return UIInterfaceOrientationMaskAll;


}

在您的视图控制器中:

- (BOOL)shouldAutorotate {
return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}

【讨论】:

    【解决方案2】:

    这是因为 UITabBarcontrollerUINavigationController 都没有将 shouldAutorotate 传递给它的可见视图控制器。要解决这个问题,您可以继承 UITabBarController 或 UINavigationController 并从那里转发 shouldAutorotate:

    在你的子类 UITabBarController 添加:

    - (BOOL)shouldAutorotate
    {
        return [self.selectedViewController shouldAutorotate];
    }
    

    在你的子类 UINavigationController 添加:

    - (BOOL)shouldAutorotate
    {
        return [self.visibleViewController shouldAutorotate];
    }
    

    【讨论】:

    • 你能解释一下“子类 UINavigationController”是什么意思吗,我知道创建一个继承自 UINavigationController 的类,但是如何处理它,当我创建它时在哪里加载它?
    • 要创建新类并将其命名为MyNavigationController(确保它的超类设置为UINavigationController)。在我之前提到的那个新创建的类中添加方法shouldAutorotate。对您的UITabBarController 执行相同操作。您应该像这样简单地使用它:MyNavigationController *nav = [[MyNavigationController alloc] initWithRootViewController:self.viewController];。与您的新 MyUITabBarController 类似。如果您使用的是界面生成器,那么只需将 Class 属性设置为指向您的新类。
    • 好的,我已经制作了自定义导航控制器并在其中添加了 shouldAutoRotate。没有 selectedViewController 只有presentedViewController。我不需要继承 UITabBarController,因为我在每个视图中只使用 UITabBarView。但是现在轮换在任何地方都不起作用。那么,如果我做得很好,如何在某些方面启用它。
    • 对于UINavigationController,您应该在我的回复中使用visibleViewController。您是否尝试过使用调试器,是否在您的自定义 UINavigationController 中调用了 shouldAutorotate
    猜你喜欢
    • 1970-01-01
    • 2012-10-01
    • 2023-04-07
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 2013-12-05
    • 1970-01-01
    相关资源
    最近更新 更多