如何在UINavigationController中的单个视图上强制使用特定的UIInterfaceOrientation?

时间:2011-05-19 21:07:26

标签: ios uinavigationcontroller rotation orientation

好的,情况就是这样:

我有一个应用程序,我只想在UINavigationController中有一个特定的视图来定位横向。这个视图是一个UIImageView,我正在捕获一个签名(这部分工作真棒)。所以,像这样:

previous view --> signature view --> next view
 (portrait)        (landscape)       (portrait)

我似乎找不到一种强制方法来强制设备方向在该签名屏幕上横向显示。在签名视图上设置纵向是没有意义的,因为在该屏幕宽度上签名的空间确实不足。

那么,关于如何实现这一点的任何好主意?我考虑过可能以模态方式进行签名视图,从而打破导航控制器。想法?

8 个答案:

答案 0 :(得分:9)

您可以尝试强制设备旋转到必要的方向 - 但您需要手动处理它(除了覆盖UIViewController方向处理方法)。

要旋转设备,您可以使用下一个方法:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

但它可能不适用于所有情况......

也可用无证方法:

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")
                                       withObject:(__bridge id)((void*)UIInterfaceOrientationLandscapeLeft)];

答案 1 :(得分:6)

只是覆盖这个UIViewController方法,只返回类似于横向的true,并且iphone将被强制旋转到该设备方向,因为它没有其他选项。

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

答案 2 :(得分:4)

不幸的是,导航控制器内的所有根UIViewControllers都必须支持他们的任何子方向。这意味着您的设置中的第一个视图控制器必须支持横向,否则子项将仅支持纵向。

实现所需内容的最佳方法是创建一个UIViewController,在旋转的变换上显示其内容视图,并将该堆栈中的所有UIViewControllers默认为纵向。

答案 3 :(得分:1)

我认为您可以在视图控制器中嵌入此视图并覆盖ShouldRotateToInterfaceOrientation方法。 祝你好运!

答案 4 :(得分:0)

要仅使用横向视图,我在ViewController中有以下内容:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

答案 5 :(得分:0)

这可能就是你要找的东西。

// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
self.view.transform = transform;

// Repositions and resizes the view.
CGRect contentRect = CGRectMake(-80, 80, 480, 320);
self.view.bounds = contentRect;

来自http://www.iphonedevsdk.com/forum/iphone-sdk-development/1394-landscape-uiviewcontroller-uiview-rotation.html

答案 6 :(得分:0)

我的应用程序只有横向视图,甚至可以从横向开始。这在iOS 5.x中运行良好,但在iOS 6.x中停止工作

在尝试了很多很多东西之后,有些东西比其他东西更值得怀疑,我找到了一个清晰且可预测的解决方案。

我做了几件事。

- 我在IB中以横向模式保留了视图。

- 我在项目设置中检查了两种横向模式 - 那里有四个图标来控制它

- iOS 6.x中的方向管理已更改。我不得不覆盖一些方法来支持改为横向

此方法适用于iOS 5.x

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    // Return YES for supported orientations.
    return (interfaceOrientation & UIInterfaceOrientationMaskLandscape);
}

这两种方法适用于iOS 6.x

- (NSUInteger)supportedInterfaceOrientations
{
    NSUInteger supportedOrientations = UIInterfaceOrientationMaskLandscape;
    return supportedOrientations;
}


- (BOOL)shouldAutorotate
{
    return YES;
}

- 但关键是要改变AppDelegate中的逻辑。我在那里的原始代码在窗口中添加了一个子视图(controller.view)。这停止在iOS 6.x中工作 - 我将调用更改为window.setRootController。这是密封它的最后一步 - 没有做出最后的改变就行不通了

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{        
    //[self.window addSubview:viewController.view];
    [self.window setRootViewController:viewController];
    [self.window makeKeyAndVisible];


    return YES;
}

答案 7 :(得分:0)

UINavigationController会覆盖包含UIViewController方向设置,因此您必须使用以下内容为5.1创建UINavigationController的自定义子类:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[self topViewController] isKindOfClass:[SigCaptureViewController class]]) {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    } else {
        return UIInterfaceOrientationIsPortrait(interfaceOrientation);
    }
}

对于6.0及以上,您需要:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    if ([[self topViewController] isKindOfClass:[EXTRASigCaptureViewController class]]) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

我还没想到的是如何使UINavigationController的旋转力。调用[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]会导致状态栏旋转,但不会导致视图旋转。