我在哪里可以设置UINavigationControllers supportedOrientations?

use*_*325 5 objective-c uinavigationcontroller ios6

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

我在哪里可以设置UINavigationControllers supportedOrientations?

根据这个http://news.yahoo.com/apple-ios-6-beta-3-changes-182849903.html,UINavigation 控制器不会咨询他们的孩子,以确定他们是否应该自动旋转.

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

如何为NavigationController设置InterfaceOrientations,以便我的ViewControllers被锁定为纵向方向?

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

谢谢你帮忙堆!

干杯!

Ant*_*ony 9

如果你想再次咨询它的孩子,你可以在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
Run Code Online (Sandbox Code Playgroud)

  • 使用类别来覆盖类的方法通常是一个坏主意,尤其是内置于操作系统中的类.改为创建一个子类. (5认同)
  • 在一个类别中覆盖这些方法的原因是糟糕/糟糕的想法是因为你绕过UINavigationController在这些方法中所做的任何事情,并将它们的实现传递给你自己的类.通过覆盖,您永远无法访问UINavigationController完成的原始实现.通过子类化,您可以调用[super someMethod].你不能在一个类别中这样做. (3认同)
  • 我同意这一点......最终取决于你的情况.这当然是一个大型项目的快速兼容性修复程序. (2认同)

小智 4

子类 UINavigationController

//OnlyPortraitNavigationController.h

@interface OnlyPortraitNavigationController : UINavigationController
Run Code Online (Sandbox Code Playgroud)

//OnlyPortraitNavigationController.m

@implementation OnlyPortraitNavigationController

- (BOOL)shouldAutorotate {
    return NO;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait; //for locked only Portrait
}
Run Code Online (Sandbox Code Playgroud)

使用您的肖像 ViewController 呈现新的子类 navigationController

SomeViewController *onlyPortraitVC = [[SomeViewController alloc]init];

OnlyPortraitNavigationController *portraitNav = [[OnlyPortraitNavigationController alloc]initWithRootViewController:onlyPortraitViewController];

[self presentViewController:portraitNav animated:YES completion:NULL];
Run Code Online (Sandbox Code Playgroud)

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