【问题标题】:Where can I set a UINavigationControllers supportedOrientations?我在哪里可以设置 UINavigationControllers supportedOrientations?
【发布时间】:2012-08-29 00:51:26
【问题描述】:

我正在使用 iOS 6.0 测试版,但我的轮换不再起作用。

在哪里可以设置 UINavigationControllers supportedOrientations?

据此http://news.yahoo.com/apple-ios-6-beta-3-changes-182849903.html UINavigation Controller 不会咨询他们的孩子来确定他们是否应该自动旋转。

我不再使用 shouldAutorotateToInterfaceOrientation:,因为它已被弃用。 相反,我使用的是supportedInterfaceOrientations:和shouldAutoRotate:并且它们工作正常,直到我将ViewController放入NavigationController(作为孩子)。 从那时起,在 ViewController 中指定的方向不再起作用。 它似乎正在使用导航控制器设置的方向(UIInterfaceOrientationMaskAllButUpsideDown)

如何设置 NavigationController 的 InterfaceOrientations 以便将我的 ViewControllers 锁定为 Portrait-Orientation?

我必须继承 UINavigationController 并在那里设置 InterfaceOrientations 吗?在 iOS 6.0 中继承 UINavigationController 不是不好的做法吗?

感谢您的帮助!

干杯!

【问题讨论】:

    标签: objective-c uinavigationcontroller ios6


    【解决方案1】:

    如果您希望它再次咨询它的孩子,您可以向 UINavigationController 添加一个类别

    @implementation UINavigationController (Rotation_IOS6)
    
    -(BOOL)shouldAutorotate
    {
        return [[self.viewControllers lastObject] shouldAutorotate];
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return [[self.viewControllers lastObject] supportedInterfaceOrientations];
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
    }
    
    @end
    

    【讨论】:

    • 使用类别来覆盖类的方法通常是个坏主意,尤其是内置于操作系统中的方法。而是创建一个子类。
    • 我同意这一点......最终这取决于你的情况。这当然是为了为大型项目提供快速的兼容性修复。
    • 为什么这是个坏主意?到目前为止,它是唯一对我有用的,而且设置起来很简单。我把它放在我的 UITabBarController 和 viola 中,我可以控制我所有视图的旋转。
    • 在一个类别中覆盖这些方法是一个坏/坏主意的原因是因为你会绕过 UINavigationController 在这些方法中所做的任何事情,并将它们的实现传递给你自己的类。通过覆盖,您将永远无法访问由 UINavigationController 完成的原始实现。通过子类化你可以调用[super someMethod]。你不能在一个类别中这样做。
    • [self.viewControllers lastObject] 只是self.topViewController
    【解决方案2】:

    子类 UINavigationController

    //OnlyPortraitNavigationController.h

    @interface OnlyPortraitNavigationController : UINavigationController
    

    //OnlyPortraitNavigationController.m

    @implementation OnlyPortraitNavigationController
    
    - (BOOL)shouldAutorotate {
        return NO;
    }
    
    -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationPortrait; //for locked only Portrait
    }
    

    用你的肖像 ViewController 呈现新的子类 navigationController

    SomeViewController *onlyPortraitVC = [[SomeViewController alloc]init];
    
    OnlyPortraitNavigationController *portraitNav = [[OnlyPortraitNavigationController alloc]initWithRootViewController:onlyPortraitViewController];
    
    [self presentViewController:portraitNav animated:YES completion:NULL];
    

    这是我的应用程序的工作,希望它可以帮助你。

    【讨论】:

      猜你喜欢
      • 2016-12-20
      • 2013-09-08
      • 2023-03-26
      • 2018-07-19
      • 2013-07-07
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多