在UIPresentationController中animation呈现视图
对于一些情况,我build议阅读这个:
非常相关的问题: “从视图控制器”消失使用UIViewControllerContextTransitioning非常相关的答案: https : //stackoverflow.com/a/25901154/751268
我试图实现一个自定义的视图控制器转换,animation新视图控制器覆盖一半的屏幕,同时收缩呈现视图控制器到90%(居中在窗口中,所提供的视图控制器下)。
首先,我的问题是viewFromKey:
返回nil
。 为了解决这个问题,答案提到:
如果你想animation呈现视图控制器的视图,你应该考虑使用
UIModalPresentationFullscreen
风格或继续使用UIModalPresentationCustom
和实现你自己的UIPresentationController
的子类与shouldRemovePresentersView
返回YES。
我做到了, viewFromKey:
不会返回nil
,但现在视图控制器完全消失(这是有道理的考虑我明确地说,它应该通过实施shouldRemovePresentersView
)。
我添加呈现视图控制器的视图到容器视图,但它仍然被删除。 还有什么我应该做的,以实现这个工作?
这里有一些相关的代码:
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey]; BOOL show = self.isPresentation; UIView *menuView = show ? toView : fromView; UIView *backView = show ? fromView : toView; UIView *containerView = [transitionContext containerView]; [containerView addSubview:backView]; [containerView addSubview:dimmedView]; [containerView addSubview:menuView]; // Adjust transforms, alpha and perform animations
我认为通过从shouldRemovePresentersView
返回YES
并手动将其添加到containerView
,这应该解决这个问题,但backView
被删除无论如何…
我正在增加另一个答案,因为我的回答太长,以至于无法置评。
首先viewForKey
在viewForKey
是可用的,所以除非你只针对iOS8(为什么?)你不应该使用它,或者在检查UIViewControllerContextTransitioning
响应那个select器并使用viewControllerForKey
for iOS7之后使用它。
就这样说,在我看来,这是一个错误,我解释我的自我:
如果你看看UIPresentationController
头文件,你会看到它说
// Indicate whether the view controller's view we are transitioning from will be removed from the window in the end of the // presentation transition // (Default: YES) - (BOOL)shouldRemovePresentersView;
所以当你看到默认是YES的时候,所以这个应该只有在你特别想说NO的时候才能覆盖。 然而,你是对的,没有明确设置为YES, viewForKey
的UITransitionContextFromViewControllerKey
保持为零。
我认为你应该填写一个错误报告,现在使用viewControllerForKey
这是罚款(没有这个错误),因为它不被弃用,并在两个操作系统版本没有问题的作品。
而这很可能是一个bug的原因是, 当 shouldRemovePresentersView
显式设置为NO和UITransitionContextFromViewControllerKey
时 , shouldRemovePresentersView
应该为viewForKey
返回一个视图。
我的2美分
为什么你将shouldRemovePresentersView
设置为YES
如果你想要的是fromView保持可见?
我和你有同样的问题,就像在我的自定义3D演示文稿中,一旦完成转换,父视图控制器就被删除。
解决方法是将modalPresentationStyle
更改为UIModalPresentationOverCurrentContext
这将特别禁止系统在转换结束时删除所有者viewController。
使用这种方法时,我的3D转换仍然受到animation问题的困扰。
我结束了使用UIModalPresentationCustom
modalPresentationStyle
,它解决了几乎所有的问题,除了新的视图控制器(这是一个UINavigationController)不会下移时,通话状态栏会出现。
为了解决这个问题,在转换完成之后 ,我最终将modalPresentationStyle
回了UIModalPresentationFullScreen
。
这里是我的代码显示新的viewController与自定义演示文稿:
//show Login Screen LoginViewController *viewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil]; UINavigationController *loginNavController = [[UINavigationController alloc] initWithRootViewController:viewController]; loginNavController.interactivePopGestureRecognizer.enabled = YES; loginNavController.transitioningDelegate = self.customTransitionDelegate; loginNavController.modalPresentationStyle = UIModalPresentationCustom; [navigationController presentViewController:loginNavController animated:YES completion:^{ //important, else status bar is not moving entire screen down.... loginNavController.modalPresentationStyle = UIModalPresentationFullScreen; } ];
另外非常重要的是,当您运行解散animation时,您不能将旧视图添加到containerView
所以如果animation是呈现,请将您的toView
添加到containerView
UIView* inView = [transitionContext containerView]; UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; ..... ...... [inView insertSubview:toViewController.view aboveSubview:fromViewController.view];
但是在解雇演示文稿时不要将视图添加到containerView
视图,因为它仍然显示(因为我们特别要求系统不要删除它),而只是在两个视图之间进行animation处理。