当modalPresentationStyle设置为UIModalPresentationCurrentContext时,没有动画效果。

12
当我使用父视图控制器 UINavigationControllermodalPresentationStyle 设置为 UIModalPresentationCurrentContext 时,展示我的 UIViewController 时没有滑入的过渡效果。
这是我的代码:
UIViewController *viewController = [[UIViewController alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
navController.navigationBarHidden = YES;

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;

[self presentViewController:navController animated:YES completion:nil];

当我不设置modalPresentationStyle时,一切都正常工作。但我需要这种样式,因为我希望UIViewController作为覆盖层呈现。

顺便说一句: 当ViewController被解除显示时,动画效果也很好。


你有没有找到解决办法? - Micky
我也遇到了同样的问题。你找到解决方案了吗? - Matt Baker
5个回答

2
根据UIViewController.h头文件的定义:-
/* 定义了在以模态方式呈现此视图控制器时将使用的转换样式。 将此属性设置为要呈现的视图控制器,而不是呈现者。 默认值为UIModalTransitionStyleCoverVertical。 */
因此,您应该在presentingViewController上应用此内容,如下所示:-
UIViewController *viewController = [[UIViewController alloc] init];

 UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
 navController.navigationBarHidden = YES;

//Here is the change
 navController.modalPresentationStyle = UIModalPresentationCurrentContext;

[self presentViewController:navController animated:YES completion:nil];

1

如果这有帮助,请告诉我,当前的视图控制器将被解除以开始动画。

UIViewController *viewController = [[UIViewController alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
navController.navigationBarHidden = YES;

self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;

[self dismissViewControllerAnimated:YES completion:^{
  [self presentViewController:navController animated:YES completion:nil];
}];

1
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
navController.navigationBarHidden = YES;

navController.modalPresentationStyle = UIModalPresentationCurrentContext;

[self presentViewController:navController animated:YES completion:nil];

使用故事板标识符初始化视图控制器可能会有所帮助。


1
如果你希望将UIViewController作为一个不是正确方式的覆盖层进行呈现,因为当你执行以下操作时: [self presentViewController:navController animated:YES completion:nil]; 你会执行一个模态呈现,而且在当前视图下方没有父视图控制器。相反,你将拥有UIWindow,所以那里可能会是黑色的,这不是你想要的。 因此,为了实现你想要的效果,你需要将你的控制器作为子视图控制器呈现,并将它的视图添加到你的父控制器视图中,像这样:
UIViewController *viewController = [[UIViewController alloc] init];

[self addChildViewController:viewController];
[self viewWillDisappear:animated];
[self.view addSubview:viewController.view];
[self.view bringSubviewToFront:viewController.view];
[viewController didMoveToParentViewController:parentController];
[self viewDidDisappear:animated];

并且要移除 UIViewController

[controller.view removeFromSuperview];
[controller willMoveToParentViewController:nil];
[controller.parentViewController viewDidAppear:animated];
[controller removeFromParentViewController];

这是错误的!如果你使用UIModalPresentationCurrentContext,底层的viewController仍然是可见的!试着通过展示一个稍微透明的viewController来验证吧! - Alexander
@Alexander 我尝试了你说的方法,但是没有效果。你能提供一些代码吗?我指的是透明度没有出现。 - Julian Osorio

1
如果您想添加覆盖层,首先需要确保您正在使用iOS 7的新ViewController转换API。以下是一个快速教程Objc.io View Controller Transitions 完成后,您应该拥有一个动画师和一个符合UIViewControllerTransitioningDelegate协议的视图控制器。
然后,当您想要呈现您的控制器时,您需要将模态呈现样式设置为UIModalPresentationStyleCustom而不是CurrentContext。自然地,您的动画师将需要配置所呈现的控制器的框架,以便仍然可以看到下面的内容。
这里是另一个可能有用的教程 - Custom presentations 最后但并非最不重要的是,您将不得不处理任何方向的演示文稿场景,如果您不这样做,当旋转时,因为过渡容器仍然处于纵向状态,您将看到奇怪的行为。请参见我的答案transitions in any orientation

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接