iOS 6 AutoRotate在UiNavigationController中

我的应用程序中有UiNavigationController。 我希望只有一个屏幕可以旋转,所以我放入这个类:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES; } -(BOOL)shouldAutorotate { return YES; } -(NSInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; } 

但发生的问题是在ecery屏幕上应用程序旋转发生。 我怎么能禁用它?

对于iOS 6,我在我的应用程序中使用以下代码,它允许您为每个viewcontroller单独指定旋转:

AppDelegate.m –

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

ViewController.m –

 - (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait; } 

最初的代码信用我相信去Ray Wenderlich“iOS 6 by Tutorials”一书。 Ray Wenderlich网站

在您要仅自动旋转的类中使用以下代码:

 @interface UITabBarController (rotation) - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; - (NSUInteger)supportedInterfaceOrientations; - (BOOL)shouldAutorotate; @end @implementation UITabBarController (rotation) - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navController = (UINavigationController *) self.selectedViewController; if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]]) return YES; } return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (NSUInteger)supportedInterfaceOrientations { if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navController = (UINavigationController *) self.selectedViewController; if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]]) return UIInterfaceOrientationMaskAll; } return UIInterfaceOrientationMaskPortrait; } - (BOOL)shouldAutorotate { if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navController = (UINavigationController *) self.selectedViewController; if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]]) return YES; } return NO; } @end 

在要旋转的类的父视图控制器(即导航控制器堆栈中的前一个类)中使用此代码。

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

在你的appDelegate中添加这段代码

 @property(nonatomic,assign)BOOL shouldRotate; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { shouldRotate=NO; } -(void)shouldAutoRotate:(BOOL)rotate { self.shouldRotate=rotate; } 

在rootview控制器中添加此代码

 #import "AppDelegate.h" #define myAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate] - (NSUInteger)supportedInterfaceOrientations { if(![myAppDelegate shouldRotate]) return UIInterfaceOrientationMaskPortrait; else return UIInterfaceOrientationMaskAllButUpsideDown; } 

之后在viewcontroller.m中添加此代码,您要旋转它

 - (void)viewDidLoad { [super viewDidLoad]; [myAppDelegate shouldAutoRotate:YES]; } -(void)viewWillDisappear:(BOOL)animated { [myAppDelegate shouldAutoRotate:NO]; } 

我为我的一个项目(IOS 7)做了这个。它对我很有用。

您可以创建一个类别来覆盖navigationController中的方法以支持所有类。

 @implementation UINavigationController (Rotation_IOS6) -(BOOL)shouldAutorotate { return [[self.viewControllers lastObject] shouldAutorotate]; } -(NSUInteger)supportedInterfaceOrientations { return [[self.viewControllers lastObject] supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; } @end 

如果要限制不同控制器中的旋转,则覆盖supportedInterfaceOrientations和shouldAutorote,在各个viewcontrollers中根据需要更改返回值。

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