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

问题描述

| 好的,这是情况: 我有一个应用程序,我只希望UINavigationController中的一个特定视图具有横向方向。这个视图是一个UIImageView,我正在捕获一个签名(该部分的功能很棒)。因此,像这样:
prevIoUs view --> signature view --> next view
 (portrait)        (landscape)       (portrait)
我似乎找不到一种很好的方法来强制设备定位在该签名屏幕上显示。在签名视图上纵向放置是毫无意义的,因为在该屏幕宽度上确实没有足够的空间进行签名。 那么,关于如何实现这一目标的任何明智的想法?我考虑过可能会以模态方式执行签名视图,因此脱离了导航控制器。有什么想法吗?     

解决方法

您可以尝试强制Device旋转到必要的方向-但是您需要手动处理它(除了覆盖UIViewController方向处理方法之外)。 要旋转设备,可以使用以下方法:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
但是在某些情况下可能无法正常工作... 也可以使用未记录的方法:
[[UIDevice currentDevice] performSelector:NSSelectorFromString(@\"setOrientation:\")
                                       withObject:(__bridge id)((void*)UIInterfaceOrientationLandscapeLeft)];
    ,只需重写此UIViewController方法即可像这样仅针对横向返回true,由于没有其他选择,iphone将被强制旋转到该设备方向。
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
    ,不幸的是,导航控制器内部的所有根UIViewController必须支持其任何子方向。这意味着您设置中的第一个视图控制器必须支持横向,否则子级将仅支持纵向。 实现您要查找的内容的最佳方法是创建一个UIViewController,在旋转的变换上显示其内容视图,并将该堆栈中的所有UIViewController默认设置为纵向。     ,我认为您可以将此视图嵌入视图控制器中,并覆盖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;
来自http://www.iphonedevsdk.com/forum/iphone-sdk-development/1394-landscape-uiviewcontroller-uiview-rotation.html     ,我有一个仅具有横向视图的应用程序,该视图甚至从横向视图开始。这在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;
}
    ,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旋转。调用ѭ11会导致状态栏旋转,但不会导致视图旋转。