本文介绍了锁定ViewController的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用程序支持所有种类的方向,但我想要某些viewControllers只支持横向和几个锁定在Potrait模式。
我如何做到,我试过下面的代码,但没有什么工作。

I have app that supports all kind of orientations, however i want certain viewControllers to support only landscape and few to lock on Potrait Mode.How can i do that, I have tried below code but nothing works.

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
// pre-iOS 6 support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}


推荐答案

同一导航控制器必须支持相同的方向。具体来说,我相信只有根视图控制器 shouldAutorotateToInterfaceOrientation 被考虑在内。您可以查看一种解决方法;或获得可能有帮助的不同方法。

All view controllers pushed on to the same navigation controller must support the same orientation. Specifically, I believe that only the root view controller shouldAutorotateToInterfaceOrientation is ever taken into account. You can have a look at this S.O. post for a sort of workaround; or at this one for a different approach that could be of help.

我链接的第二篇文章建议使用 shouldAutorotateToInterfaceOrientation 这个定义,你可能会为你的情况调整它: p>

The second post I linked above suggests using this definition for shouldAutorotateToInterfaceOrientation you might tweak it for your case:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
  if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
    UIViewController *rootController = [((UINavigationController *)self.selectedViewController).viewControllers objectAtIndex:0];
    return [rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
  }
  return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

这篇关于锁定ViewController的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 14:58