使用NavigationController在不同视图上的不同方向

我已经subClassed我的NavigationController并添加了这段代码:

-(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft |UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeLeft; } 

我有2个视图控制器。 如何在不能旋转的情况下在一个和纵向模式下强制横向模式? (所有标志都在plist中启用)

假设您正在使用iOS6,请更改您的自定义导航控制器以实现此function(此解决方案似乎仅适用于模态或全屏演示):

  - (NSUInteger)supportedInterfaceOrientations { return self.topViewController.supportedInterfaceOrientations; } 

  - (BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } 

然后在您想要添加Landscape的控制器中

  - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; } - (BOOL)shouldAutorotate { return YES; } 

而在另一方面

  - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (BOOL)shouldAutorotate { return YES; } 

我正在考虑仅针对iOS 6.0及更高版本进行轮换。 为UINaviagtionController创建类别可能是一个很好的方法。 请按照以下步骤操作

步骤1.将此代码放入AppDelegate.m中

  - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskAll; } 

步骤2.为UINaviagtionController创建一个类别

Orientation.h

  #import  @interface UINavigationController (Orientation) @end 

Orientation.m

  #import "Orientation.h" @implementation UINavigationController (Orientation) - (BOOL)shouldAutorotate { if (self.topViewController != nil) return [self.topViewController shouldAutorotate]; else return [super shouldAutorotate]; } - (NSUInteger)supportedInterfaceOrientations { if (self.topViewController != nil) return [self.topViewController supportedInterfaceOrientations]; else return [super supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { if (self.topViewController != nil) return [self.topViewController preferredInterfaceOrientationForPresentation]; else return [super preferredInterfaceOrientationForPresentation]; } 

步骤3.现在创建一个强制初始方向的UIViewController类。 对于肖像

PortraitVC.h

  #import  @interface PortraitVC : ViewController @end 

PortraitVC.m

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

同样适用于景观

LandscapeVC.h

  #import  @interface LandscapeVC : ViewController @end 

LandscapeVC.m

  - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeLeft; } 

步骤4.现在您想要仅在纵向模式下强制而不进行旋转的原始ViewController,将此代码写入viewDidLoad

  PortraitVC *c = [[PortraitVC alloc] init]; [self presentViewController:c animated:NO completion:NULL]; [self dismissViewControllerAnimated:NO completion:NULL]; 

并设置这样的方向

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

如果您只想在横向模式下强制原始ViewController而没有旋转,请在viewDidLoad中编写此代码

  LandscapeVC *c = [[LandscapeVC alloc] init]; [self presentViewController:c animated:NO completion:NULL]; [self dismissViewControllerAnimated:NO completion:NULL]; 

并设置这样的方向

  - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeLeft; } 

我之所以这样做是因为只有在使用presentViewController或presentModalViewController时才会调用“preferredInterfaceOrientationForPresentation”。 并且要设置初始方向,必须调用“preferredInterfaceOrientationForPresentation”。

希望这可以帮助。

所以经过很多小道和错误之后我找到了一个可以解决的问题。

在你的AppDelegate中:

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

这将控制根视图控制器的方向。

在您的DataManager.h中 (或者您正在使用的任何其他单例)也可以使用NSUserDefaults完成。

 @property (nonatomic, strong) NSString *currentView; 

在您的ViewController.m(root)中:

 -(NSUInteger)supportedInterfaceOrientations { if([[DataManager sharedDataManager].currentView isEqualToString:@"Trailer"]) { return UIInterfaceOrientationMaskLandscapeRight; } else if([[DataManager sharedDataManager].currentView isEqualToString:@"Menu"]) { return UIInterfaceOrientationMaskPortrait; } return UIInterfaceOrientationMaskPortrait; } 

从根ViewController,您可以访问应用程序中所有ViewControlles的所有方向。 如果要将一个ViewController限制为横向,另一个仅限于纵向,则非常有用。 您需要添加到每个ViewController的唯一内容是在ViewWillAppear ,您要为ViewController提供名称,如下所示:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [DataManager sharedDataManager].currentView = @"Menu"; } 

使用此解决方案,您将无法获得有关在视图之间切换的恼人警告,就像您使用当前\ dissmis视图解决方案强制轮换一样。 也许有一种方法可以更好地识别ViewController,我很想听听。 如果您发现此解决方案有任何问题,我也很想听听。 谢谢