2011-05-19 55 views
21

好吧,所以情况如下:如何在UINavigationController的单个视图上强制特定的UIInterfaceOrientation?

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

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

我似乎无法找到一种很好的方法来强制设备的方向在该签名屏幕上横向。在签名视图上设置纵向方向永远不会有意义,因为实际上没有足够的空间来签署该屏幕宽度。

那么,如何做到这一点什么好主意?我已经考虑过模态地进行签名视图,因此突破了导航控制器。思考?

+0

这是否有诀窍? – Nirma 2011-08-29 18:51:48

+0

在我有机会解决问题之前,该项目因预算原因被终止。如果任何答案听起来像是我所寻找的东西,那么Epic_orange和mdizzle的答案就是使用视图转换策略。 Nirma的解决方案是每个人似乎都回应的标准票价。但是,这不适用于UINavigationController上下文中的理想结果。试试看:用三个视图设置UINav ...做第二个总是风景。由于我没有验证,因此无法接受答案,并且可能不会。这些天过于忙于ASP.NET MVC。 – NovaJoe 2011-08-30 19:19:14

回答

1

我觉得你可以嵌入一个视图控制器内的这一观点,并覆盖ShouldRotateToInterfaceOrientation方法。 祝你好运!

0

要使用仅景观查看,我在的ViewController以下几点:

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

但是这并不强制一个方向的观点。这是一个愚蠢的问题。当你在UINavigationController的上下文中,关于视图布局和状态栏方向会发生奇怪的事情。我从来没有深究过,但也不在乎,因为该项目因其他原因被取消。 – NovaJoe 2011-08-22 14:34:46

4

不幸的是,里面的导航控制器的所有根UIViewControllers必须支持所有孩子的取向。这意味着您的设置中的第一个视图控制器必须支持横向,否则小孩将只支持纵向。

实现你正在寻找的最好方法是创建一个UIViewController,它在旋转的变换上显示其内容视图,并且只是将该栈中的所有UIViewControllers默认为肖像。

+0

如何实现*在旋转变换*上显示其内容视图?你有这样的例子吗? – testing 2015-09-24 12:13:48

6

只需重写此方法的UIViewController只为景观,像这样和iPhone将被迫旋转到该设备的方向返回true,因为它没有其他选择。

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

这只适用于UIViewController不是导航控制器的一部分。你必须把这段代码放在UINavigationController中,然后检查topViewController是否是你在横向的。 – Peter 2013-05-30 22:09:55

9

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

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

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; 

但它可能不会在所有情况下工作...

也可无证的方法:

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

你有没有在UINavigationViewController中工作?当我这样做时,它会旋转状态栏而不是控制器。 – Peter 2013-05-30 22:18:45

0

我有一个只有横向景观的应用程序,甚至开始景观。这是工作中的iOS 5.x的罚款,但在iOS版6.x的

不辍努力很多很多事情后,一些比其他人更可疑的,我发现了一个解决方案,对我来说是明确的和可预测的。

我做了几件事。

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

- 我在项目设置检查两个横向模式 - 一个有四个图标有控制它

- 定向MGMT已经在iOS版6.x的改变我不得不重写一些方法,以支持不断变化的景观

这种方法适用于iOS 5.x的

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

这2种方法都适用于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; 
} 
0

的UINavigationController的覆盖包含的UIViewController方向设置工作,所以你必须创建的UINavigationController的自定义子类与5.1如下:

- (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; 
    } 
} 

我还没有想出什么是如何使力UINA vigationController进行旋转。调用[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]会导致状态栏旋转但不会导致视图旋转。

相关问题