presentViewController不支持iOS 6中的方向
我正在使用这个代码
if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) { [self presentViewController:navigationControllerCustom animated:YES completion:nil]; } else { [self presentModalViewController:navigationControllerCustom animated:YES]; }
我的应用程序有两个方向肖像和肖像颠倒。 此代码适用于iOS 5.1,但在iOS 6上方向不起作用
我还在我的navigationControllerCustom
类中添加了这段代码
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); } -(NSUInteger)supportedInterfaceOrientations { return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown); }
请帮我解决这个问题。
提前致谢。
你必须在你的应用程序委托中包含这个:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; }
还要确保视图控制器都具有以下,对我来说工作正常。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; }
该文件还说, UINavigationController's
不会查询其支持的方向的顶部视图控制器,虽然在开发者论坛上的苹果工程师确实这样说…似乎并没有。 因此,您应该为UINavigationController
添加一个类别,这是我使用的一个:
#import "UINavigationController+autorotate.h" @implementation UINavigationController (autorotate) - (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; } @end
有关AutoRotate如何在iOS 6上运行的更多信息, 请查看此答案
你忘了:
- (BOOL)shouldAutorotate { return YES; }
只需将代码片段粘贴到该代码上方即可
@implementation UINavigationController (rotation) //temp hack for iOS6, this allows passing supportedInterfaceOrientations to child viewcontrollers. - (NSUInteger)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } @end
如果您打算启用或禁用所有视图控制器的旋转,则不需要inheritanceUINavigationController。 而是使用:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
在你的AppDelegate。
如果您打算在您的应用中支持所有方向,但是在PARENT View Controller(例如UINavigationController堆栈)上您应该使用不同的方向
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
结合您的父视图控制器中的以下方法。
- (BOOL)shouldAutorotate
和
- (NSUInteger)supportedInterfaceOrientations
但是如果你打算在不同的CHILDREN ViewControllers中有不同的方向设置(像我一样),你需要在导航栈中检查当前的ViewController。
我在我的UINavigationController子类中创build了以下内容:
- (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { int interfaceOrientation = 0; if (self.viewControllers.count > 0) { id viewController; DLog(@"%@", self.viewControllers); for (viewController in self.viewControllers) { if ([viewController isKindOfClass:([InitialUseViewController class])]) { interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; } else if ([viewController isKindOfClass:([MainViewController class])]) { interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; } else { interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown; } } } return interfaceOrientation; }
既然你不能再控制ViewController的子ViewControllers的旋转设置,你必须以某种方式拦截当前在导航栈中的视图控制器。 所以这就是我所做的:)。 希望有所帮助!
子类UINavigationController并重写shouldAutorotate和supportedInterfaceOrientations,如下所示:
-(BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } -(NSUInteger)supportedInterfaceOrientations { return self.topViewController.supportedInterfaceOrientations; }
而现在你可以控制你的方向是每个ViewController。 只需重写每个ViewController中的两个方法。