iOS 7.仅更改一个视图控制器的页面方向

我有iPhone应用程序,只支持肖像方向。 我想添加到我的项目视图控制器,将只支持横向方向? 可能吗? 如果是的话我怎么能做到这一点?

我试图把这个类文件打包成这样:

@implementation UINavigationController (Rotation_IOS7) -(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } 

如果我这样做,我得到这个错误:终止应用程序由于未捕获的exceptionUIApplicationInvalidInterfaceOrientation ,原因: Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES

我已经试过这个,它的工作原理: http : //www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

首先,将这些代码添加到您的AppDelegat类。

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { // Get topmost/visible view controller UIViewController *currentViewController = [self topViewController]; // Check whether it implements a dummy methods called canRotate if ([currentViewController respondsToSelector:@selector(canRotate)]) { // Unlock landscape view orientations for this view controller return UIInterfaceOrientationMaskAllButUpsideDown; } // Only allow portrait (standard behaviour) return UIInterfaceOrientationMaskPortrait; } - (UIViewController*)topViewController { return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; } - (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController { if ([rootViewController isKindOfClass:[UITabBarController class]]) { UITabBarController* tabBarController = (UITabBarController*)rootViewController; return [self topViewControllerWithRootViewController:tabBarController.selectedViewController]; } else if ([rootViewController isKindOfClass:[UINavigationController class]]) { UINavigationController* navigationController = (UINavigationController*)rootViewController; return [self topViewControllerWithRootViewController:navigationController.visibleViewController]; } else if (rootViewController.presentedViewController) { UIViewController* presentedViewController = rootViewController.presentedViewController; return [self topViewControllerWithRootViewController:presentedViewController]; } else { return rootViewController; } } 

然后,在你的横向视图控制器中,添加这个方法

 - (void)canRotate { } 

我search了很多主题,最终find了一个可行的解决scheme。

在我的例子中,我有两个VC:

一个 – > VC内嵌的导航。 控制器,应该只支持肖像视图。

B – >没有embeddedVC内部的VC,应该只支持Landscape。

我想从视图A去查看B(通过按一个button),然后返回查看,然后A的具体方向仍然正确。

I.为UINavigationController创build一个类别,并在.m文件中写入以下内容:(将自动调用该代码)

 - (NSUInteger)supportedInterfaceOrientations { NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController supportedInterfaceOrientations]); return [self.topViewController supportedInterfaceOrientations]; } - (BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // You do not need this method if you are not supporting earlier iOS Versions return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; } 

II。 在A和B之间创build一个模态继续,然后在B和A之间创build一个模态继续。

III。 在每个视图控制器.m文件中记下以下内容:

 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } 

要么

 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

添加此代码后。 您将能够更改单个视图B的方向

编辑:

在.h中创build一个类别,然后实现这些方法

在视图控制器中使用这些方法来支持风景

 @implementation UINavigationController (Rotation_IOS7) -(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; }