首页 文章

iOS 6:旋转后忽略父模态的modalPresentationStyle

提问于
浏览
33

使用iOS6的iPad,我们有这个错误,即模态视图控制器将扩展到全屏,即使它被告知使用“表单”演示样式 . 但是,只有当有两个模态,一个父模型及其子模式时,才会发生这种情况 .

这就是第一个模态的创建和呈现方式:

UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[parentController presentModalViewController:navigationController animated:YES];
// parentController is my application's root controller

这是创建和呈现子模态的方式:

UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[parentController presentModalViewController:navigationController animated:YES];
// parentController is the navigationController from above

因此,当从横向旋转到纵向时,即使我们旋转回横向,父模式也会扩展到全屏并保持这种状态 .

当我们拥有父模态本身(没有子模态)时,它按预期工作,即它保持在表单样式中 .

请注意,这只发生在iOS6(设备和模拟器)上,并且不会发生在iOS 5(模拟器上,并报告由测试人员工作) .

到目前为止,我已经尝试了以下但没有成功:

  • 设置 wantsFullScreenLayoutNO

  • 强制 wantsFullScreenLayout 总是通过覆盖它来返回 NO

  • 在导航控制器中确定我的控制器也指定 UIModalPresentationFormSheet

  • 实施 preferredInterfaceOrientationForPresentation

  • 升级到iOS 6.0.1

谢谢!


UPDATE :所以,我调整了Apple Developer Forums(https://devforums.apple.com/message/748486#748486)的响应,以便它适用于多个嵌套模式 .

- (BOOL) needNestedModalHack {
    return [UIDevice currentDevice].systemVersion.floatValue >= 6;
}

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                          duration:(NSTimeInterval)duration {

    // We are the top modal, make to sure that parent modals use our size
    if (self.needNestedModalHack && self.presentedViewController == nil && self.presentingViewController) {
        for (UIViewController* parent = self.presentingViewController;
             parent.presentingViewController;
             parent = parent.presentingViewController) {
            parent.view.superview.frame = parent.presentedViewController.view.superview.frame;
        }
    }

    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                 duration:(NSTimeInterval)duration {
    // We are the top modal, make to sure that parent modals are hidden during transition
    if (self.needNestedModalHack && self.presentedViewController == nil && self.presentingViewController) {
        for (UIViewController* parent = self.presentingViewController;
             parent.presentingViewController;
             parent = parent.presentingViewController) {
            parent.view.superview.hidden = YES;
        }
    }

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    // We are the top modal, make to sure that parent modals are shown after animation
    if (self.needNestedModalHack && self.presentedViewController == nil && self.presentingViewController) {
        for (UIViewController* parent = self.presentingViewController;
             parent.presentingViewController;
             parent = parent.presentingViewController) {
            parent.view.superview.hidden = NO;
        }
    }

    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}

3 回答

  • 0

    不确定这是否应该被视为一个错误,我很好奇iOS 7将带来什么,但目前这个问题的解决方法是将modalPresentationStyle设置为子视图控制器的UIModalPresentationCurrentContext .

    Set modalPresentationStyle = UIModalPresentationCurrentContext
    

    这使得孩子仍然可以作为FormSheet呈现,但是防止父母在轮换时调整大小到全屏 .

    短剑

  • -1

    我在这里可以看到2个问题 .

    1)在iOS 6中,方法 presentModalViewController:animated: 已弃用,请尝试使用 presentViewController:animated:completion: (尽管这可能没有帮助,但您仍可能想要这样做)

    2)在iOS 6中,不知何故,容器控制器(例如 UINavigationController )不会向自己的子节点重新发送自动调整消息 . 尝试继承 UINavigationController 并重新定义相应的自动旋转方法以发送给所有子项 . 这可能有所帮助 .

  • 7

    您需要在主视图后实例化导航控制器 . 这样您就可以在每个视图中管理旋转 .

    如果您的AppDelegate RootViewController是导航控制器,您将无法使用本机功能管理旋转 .

相关问题