子类UINavigationController中的shouldAutorotate返回NO但仍然发生旋转

时间:2014-05-06 19:41:05

标签: ios ios7 rotation orientation

我有一个视图控制器,我想以纵向模式锁定。通过创建自定义导航控制器,我可以在设备处于横向状态时强制执行纵向模式,并且最初在应用启动时起作用。

但是当我将设备旋转到纵向模式然后再回到横向时,方向会再次改变。

我认为如果返回NO应该会阻止这种情况,但由于某些原因它并不存在。我确认每次实际旋转设备时都会调用该函数。

任何人都知道为什么iOS忽略了我的NOAutotate?

我已经阅读过很多帖子,我的代码似乎是很多人的建议,但是没有用。

@interface MyUINavigationController : UINavigationController

@end

@implementation MyUINavigationController

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait ; 
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
     NSLog(@"shouldAutorotate called and returning NO");
     return NO;
}

@end

// This is how I am using my custom Navigation Controller:

UINavigationController* mynav = [[MyUINavigationController alloc] initWithRootViewController:customViewController];
visibleVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[mynav setModalPresentationStyle:UIModalPresentationFullScreen]; 
[mynav setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 

[visibleVC presentViewController:mynav animated:YES completion:nil];

0 个答案:

没有答案