支持不同的方向只有一个视图的iOS 6

我只想旋转我的应用程序中的一个我的意见,左或右风景。 我所有的其他意见是在肖像模式,我已经设置我的应用程序只支持肖像模式。 在iOS 6中改变方向,我不知道如何做到这一点。 我已经尝试下面张贴如下。 谁能告诉我我做错了什么? 谢谢!

-(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; } -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; } 

我也试过了:

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; return YES;//UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; } -(void)didRotate:(NSNotification *)notification { UIDeviceOrientation orientation = [[notification object] orientation]; if (orientation == UIDeviceOrientationLandscapeLeft) { [theImage setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)]; [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)]; } else if (orientation == UIDeviceOrientationLandscapeRight) { [theImage setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)]; [self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)]; } else if (orientation == UIDeviceOrientationPortraitUpsideDown) { [theImage setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)]; [self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)]; } else if (orientation == UIDeviceOrientationPortrait) { [theImage setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)]; [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)]; } } 

这对我有用如何在iOS 6强制UIViewController纵向方向

从UINavigationController创build一个新的类别,覆盖旋转方法:

 -(BOOL)shouldAutorotate { return [self.topViewController shouldAutorotate]; } -(NSUInteger)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.topViewController preferredInterfaceOrientationForPresentation]; } @end 

问候!

iOS 6中有关于处理视图旋转的更改。 支持Info.plist中定义的方向。 即使你正在返回其他的。

尝试select您的项目中支持的所有方向。

处理视图旋转

在iOS 6中,您的应用程序支持在应用程序的Info.plist文件中定义的界面方向。 视图控制器可以覆盖supportedInterfaceOrientations方法来限制支持的方向列表。 通常,系统仅在窗口的根视图控制器或呈现的视图控制器上调用此方法以填充整个屏幕; 子视图控制器使用父视图控制器为其提供的窗口部分,不再直接参与有关支持哪些旋转的决策。 应用程序的方向遮罩和视图控制器的方向遮罩的交集用于确定视图控制器可以旋转到哪个方向。

您可以覆盖旨在以特定方向全屏显示的视图控制器的preferredInterfaceOrientationForPresentation

在iOS 5和更早的版本中, UIViewController类仅在纵向模式下显示视图。 为了支持额外的方向,你必须重写shouldAutorotateToInterfaceOrientation:方法,并返回你的子类支持的任何方向。 如果视图的自动调整属性configuration正确,那可能就是你所要做的。 但是, UIViewController类提供了额外的钩子,可以根据需要实现其他行为。 一般来说,如果你的视图控制器是用来作为一个子视图控制器,它应该支持所有的界面方向。

当可见视图控制器发生旋转时,将在旋转过程中调用willRotateToInterfaceOrientation:duration :, willAnimateRotationToInterfaceOrientation:duration:didRotateFromInterfaceOrientation:方法。 viewWillLayoutSubviews方法也在视图resize并由父级定位后调用。 如果在发生方向更改时视图控制器不可见,则不会调用旋转方法。 但是,视图变为可见时,将调用viewWillLayoutSubviews方法。 这个方法的实现可以调用statusBarOrientation方法来确定设备方向。

(C) Apple Docs:UIViewController

按照以下步骤

  • 创buildUINavigationController的子类覆盖旋转方法。
  • 在AppDelegate中,创build一个BOOL islandscape属性。
  • 当一个视图被按下/放大/显示/解除时,调整这个BOOL值。

示例项目

我为此创build了一个完美的示例项目。 下载并整合到您的项目中: https : //www.dropbox.com/s/nl1wicbx52veq41/RotationDmeo.zip?dl = 0

我有一个:

TabbarController – > NavigationController – > ViewController – > ViewController

我分类UITabBarController并添加….

 -(NSUInteger)supportedInterfaceOrientations{ if (self.selectedIndex >= 0 && self.selectedIndex < 100) { for (id vC in [[self.viewControllers objectAtIndex:(unsigned long)self.selectedIndex] viewControllers]) { if ([vC isKindOfClass:[CLASS_WHICH_SHOULD_ALLOW class]]) { return UIInterfaceOrientationMaskPortrait + UIInterfaceOrientationMaskLandscape; } } } return UIInterfaceOrientationMaskPortrait; } 

我一直在寻找解决scheme几个小时!

所以到处实施所需的方法之后。 shouldAutorotate不需要设置为YES因为它已被设置为默认值:

 - (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return UIInterfaceOrientationPortrait; } 

当显示UIViewController需要与其他视图不同的方向的时候,我在里面创build了一个UIStoryboardSegue

 #import "Showing.h" @implementation Showing - (void)perform{ NSLog(@"Showing"); UIViewController *sourceVC = self.sourceViewController; UIViewController *presentingVC = self.destinationViewController; [sourceVC.navigationController presentViewController:presentingVC animated:YES completion:nil]; } @end 

UIStoryboard里面我用这个segue( 显示 )连接了视图:

在这里输入图像说明

这很重要,你正在使用

 presentViewController:animated:completion: 

并不是

 pushViewController:animated: 

否则方向将不会再被确定。


我一直在尝试像

 [UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; 

或者UIViewController里面的方向应该改变的地方,我也UIStoryboardSeguespresentingViewController dismissViewControllerdismissViewController之前在我的自定义UIStoryboardSegues调用它:

 [UIViewController attemptRotationToDeviceOrientation]; 

要么

 NSNumber *numPortrait = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:numPortrait forKey:@"orientation"]; 

但没有一个人工作。 当然,最后一个例子不应该是一个选项,因为如果苹果将改变他们的API的任何东西,这可能会导致您的应用程序内的问题。

我也尝试使用AppDelegate方法,并且在查找实际的visibleViewController的正确UIInterfaceOrientation后,总是确定此方法内的方向,但是当从一个方向切换到另一个方向时,有时会发生崩溃。 所以我仍然想知道为什么它变得如此复杂,似乎也没有任何文件解释正确。 即使下面这部分没有帮助我。

的UIViewController + OrientationPermissions.h

 @interface UIViewController (OrientationPermissions) + (void)setSupportedOrientations:(UIInterfaceOrientationMask)supportedOrientations; + (UIInterfaceOrientationMask)supportedOrientations; @end 

的UIViewController + OrientationPermissions.m

 @implementation UIViewController (OrientationPermissions) static UIInterfaceOrientationMask _supportedOrientations; + (void)setSupportedOrientations: (UIInterfaceOrientationMask)supportedOrientations { _supportedOrientations = supportedOrientations; } + (UIInterfaceOrientationMask)supportedOrientations { return _supportedOrientations; } @end 

在你的UIApplication委托中

 - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return [UIViewController supportedOrientations]; } 

然后在所需的视图控制器上做类似的事情

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [UIViewController setSupportedOrientations:UIInterfaceOrientationMaskAll]; } 

离开这个视图控制器之前不要忘记重置掩码

请注意,如果您使用UINavigationController或UITabBarController,请参阅https://stackoverflow.com/a/28220616/821994如何绕&#x8FC7;

勉强工作请尝试。 我2天后解决

//AppDelegate.m – 这个方法在iOS6之前是不可用的

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown; if(self.window.rootViewController){ UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject]; orientations = [presentedViewController supportedInterfaceOrientations]; } return orientations; } 

//MyViewController.m – 返回每个UIViewController支持的方向

 - (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait; }