如何在UINavigationController的单个视图上强制特定的UIInterfaceOrientation?
好的,情况如下:
我有一个应用程序,我只想在UINavigationController中有一个特定的视图具有横向。 这个视图是一个UIImageView,我正在捕获一个签名(这部分工作真棒)。 所以,像这样:
previous view --> signature view --> next view (portrait) (landscape) (portrait)
我似乎无法find一种很好的方法来强制设备方向横向签名屏幕上。 在签名视图上使用纵向方向永远不会有意义,因为实际上没有足够的空间来签署该屏幕宽度。
那么,关于如何做到这一点的任何明智的想法? 我已经考虑可能做模式签名视图,从而打破了导航控制器。 思考?
您可以尝试强制设备旋转到必要的方向 – 但您需要手动处理(除了覆盖UIViewController方向处理方法)。
要旋转设备,您可以使用下一个方法:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
但在这可能无法在所有情况下工作…
还有无证的方法:
[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(__bridge id)((void*)UIInterfaceOrientationLandscapeLeft)];
只要重写这个UIViewController方法,只返回真实的风景如此,iPhone将被迫旋转到该设备的方向,因为它没有其他的select。
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); }
不幸的是,导航控制器内的所有根UIViewController都必须支持它们的任何子方向。 这意味着您的设置中的第一个视图控制器必须支持横向,否则孩子将只支持纵向。
实现你正在寻找的最好的方法是创build一个UIViewController,在旋转的转换上显示它的内容视图,并且只是将所有的UIViewControllers默认为纵向。
我认为你可以在视图控制器中embedded这个视图,并覆盖ShouldRotateToInterfaceOrientation方法。 祝你好运!
要在景观中使用View,我在ViewController中有以下几点:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft); }
这可能是你要找的。
// 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;
我有一个应用程序,只有横向景观,甚至开始景观。 这在iOS 5.x中工作正常,但在iOS 6.x中停止工作
在尝试了很多事情之后,还有一些比别人更可疑,我find了一个对我来说是清晰可预见的解决scheme。
我做了几件事情。
– 我在IB保留了景观模式的意见。
– 我在项目设置中检查了两个风景模式 – 那里有四个图标来控制它
– iOS 6.x中的方向pipe理已更改。 我不得不改写一些方法来支持改变风景
此方法适用于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; }
UINavigationController覆盖了包含UIViewController方向设置,所以你必须创build一个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; } }
我没有想到的是如何使UINavigationController旋转的力量。 调用[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]
会导致状态栏旋转,但不会导致视图旋转。