繁体   English   中英

在iOS6中,将ViewController推入堆栈时强制将ViewController强制为特定的interfaceOrientation

[英]In iOS6, trouble forcing ViewController to certain interfaceOrientation when pushed on stack

我设置了以下视图控制器:

viewController1可以自由旋转到除了人像倒置之外的任何方向。

viewController2被推到viewController1的顶部,我希望它与viewController1的方向相同,并且我希望它不能旋转。

viewController3被推到viewController2的顶部。 我希望viewController3处于纵向模式。

我在尝试在iOS6中完成此操作时遇到了很多问题(在iOS5中尚未尝试过)。 首先,我已经创建了自己的导航控制器,并将以下内容放入其中:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

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

- (BOOL) shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

我尝试了这些东西的许多不同组合,以了解是否有用。 如果vc2是横向的,我主要是在强迫vc3以纵向显示。 任何帮助,将不胜感激。

您要在此处进行的工作是在对抗框架。 您所描述的并不是导航控制器体系结构在iOS 6中的工作原理。如果要显示视图控制器的视图并强制旋转,请使用显示的视图控制器。 这是preferredInterfaceOrientationForPresentation唯一有意义的时间,并且实际上将查询您的视图控制器的supportedInterfaceOrientations ,因为呈现时它将位于接口的根部。

我在另一个答案中解释说,在iOS 6中不支持将新的视图控制器推到导航控制器上时强制旋转。 您可以构建有关补偿旋转的规则(即,如果用户旋转设备会发生什么),但是您不能强制界面旋转。 iOS 6乐于强迫您旋转的唯一情况是在呈现或关闭视图控制器时( presentViewController:animated:dismissViewControllerAnimated:

然而,可以以这样的方式使用呈现视图控制器,它那种看起来就像你推到导航控制器。 我拍了一部电影,说明了我的意思:

http://youtu.be/O76d6FhPXlE

现在,这绝对不是完美的。 状态栏没有旋转动画,并且两个视图之间有一种黑色的“闪烁”-这是有意的,因为它可以掩盖实际情况 真正发生的是,实际上有两个不同的导航控制器和三个视图控制器,如故事板的屏幕截图所示。

在此处输入图片说明

我们拥有的是:

  • 设置为纵向方向的导航控制器子类及其根视图控制器

  • 第二个导航控制器子类设置为横向,其根视图控制器为黑色,用作中介

  • 第三个视图控制器将被推到第二个导航控制器的堆栈上

当用户要求从第一个视图控制器“前进”时,我们显示第二个导航控制器,从而瞬间看到黑色视图控制器,但是随后我们立即按下了第三个视图控制器。 因此,我们得到了强制旋转以及一种黑色闪光和推动动画。 当用户点击第三个视图控制器中的“后退”按钮时,我们将撤消该过程。

所有过渡代码都在黑色视图控制器(ViewControllerIntermediary)中。 我尝试对其进行调整,以提供最令人满意的动画:

@implementation ViewControllerIntermediary {
    BOOL _comingBack;
}

- (void) viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
}

-(void)navigationController:(UINavigationController *)nc 
        willShowViewController:(UIViewController *)vc 
        animated:(BOOL)anim {
    if (self == vc)
        [nc setNavigationBarHidden:YES animated:_comingBack];
    else
        [nc setNavigationBarHidden:NO animated:YES];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (!_comingBack) {
        [self performSegueWithIdentifier:@"pushme" sender:self];
        _comingBack = YES;
    }
    else
        [self.navigationController dismissViewControllerAnimated:YES 
            completion:nil];
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([self.window.rootViewController.presentedViewController isKindOfClass: [SecondViewController class]])
{
    SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController;

    if (secondController.isPresented)
        return UIInterfaceOrientationMaskAll;
    else return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskPortrait;
}

对于斯威夫特

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {

    if self.window?.rootViewController?.presentedViewController? is SecondViewController {

        let secondController = self.window!.rootViewController.presentedViewController as SecondViewController

        if secondController.isPresented {
            return Int(UIInterfaceOrientationMask.All.toRaw());
        } else {
            return Int(UIInterfaceOrientationMask.Portrait.toRaw());
        }
    } else {
        return Int(UIInterfaceOrientationMask.Portrait.toRaw());
    }

}

有关更多详细信息,请检查此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM