使用关闭各种视图控制器时,关闭视图控制器后UIModalPresentationCustom
,屏幕将变黑,就好像所有视图控制器都已从视图层次结构中删除一样。
正确设置了过渡委托,要求并正确传递animationControllerForPresentedController,并且一旦动画结束,过渡就完成了。
使用iOS 7 SDK编译时,此确切的代码可以完美地工作,但使用iOS 8b5编译时,该代码会损坏
使用关闭各种视图控制器时,关闭视图控制器后UIModalPresentationCustom
,屏幕将变黑,就好像所有视图控制器都已从视图层次结构中删除一样。
正确设置了过渡委托,要求并正确传递animationControllerForPresentedController,并且一旦动画结束,过渡就完成了。
使用iOS 7 SDK编译时,此确切的代码可以完美地工作,但使用iOS 8b5编译时,该代码会损坏
Answers:
这是因为您很可能同时添加两个演示文稿
[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]
和提出
[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]
在您的动画控制器的(void)animateTransition:(id)transitionContext方法中查看容器的控制器。由于您使用的是自定义模式演示,因此呈现视图控制器仍显示在呈现的视图控制器下方。现在,由于它仍然可见,因此您无需将其添加到容器视图中。而是仅将提供的视图控制器添加到containerView。应该在animateTransition:方法内部看起来像这样
UIView *containerView = [transitionContext containerView];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// Boolean value to determine presentation or dismissal animation
if (self.presenting){
[transitionContext.containerView addSubview:toViewController.view];
// Your presenting animation code
} else {
// Your dismissal animation code
}
UIViewControllerContextTransitioning
标题,我们应该使用-viewForKey
方法来获取视图。不幸的是,在某些情况下,UIModalPresentationCustom
原因导致的表示形式-viewForkey
为零,这显然是所需的行为....bcherry
在此处查看答案。
这种高票赞成的答案会误导人们。长话短。
首先,不要使用UIModalPresentationCustom,这听起来并不像。(详细)
其次,有一种新方法可以在animateTransition中从视图中检索/到视图,不再使用诸如'fromVC.view'之类的东西。(为什么)
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
//swift
let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
现在黑屏应该消失了。
似乎我遇到了相同的问题,我正在使用Xcode 6 beta5。
我在Google上进行搜索,发现其他人也遇到了同样的问题,他们说这是iOS 8中的一个严重错误,因此希望Apple可以尽快解决此问题。
https://github.com/TeehanLax/UIViewController-Transitions-Example/issues/5
我将下面的代码添加到过渡完成块中,并为我修复了它。
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations: ^{
// Animation code
} completion: ^(BOOL finished) {
// More of your code
// Add the following line before completing the transition
[[[UIApplication sharedApplication] keyWindow] sendSubviewToBack:toViewController.view];
// Complete the transition
[transitionContext completeTransition:YES];
}];
可能视图层次结构与新的Xcode无关,或者与iOS8有所不同。这段代码对我有用。在animateTransition:transitionContext方法中关闭控制器时添加它。
[[UIApplication sharedApplication].keyWindow addSubview:toViewController.view];
toViewController.view.userInteractionEnabled = YES;
快速提示:确保您的“ From” UIViewController是您所期望的。
NSLog(@" FROM vc %@" , [[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey] description]);
过去,我的代码中也有类似的错误。您可以轻松地提取正确的“ From” VC上下文。
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
fromView = [[[fromVC childViewControllers] firstObject] view];
我有同样的问题,对我来说造成问题的是我没有设置toViewController的最后一帧。请参见http://www.appcoda.com/custom-view-controller-transitions-tutorial/中的以下示例
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let finalFrameForVC = transitionContext.finalFrameForViewController(toViewController)
let containerView = transitionContext.containerView()
let bounds = UIScreen.mainScreen().bounds
toViewController.view.frame = CGRectOffset(finalFrameForVC, 0, bounds.size.height)
containerView.addSubview(toViewController.view)
UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .CurveLinear, animations: {
fromViewController.view.alpha = 0.5
toViewController.view.frame = finalFrameForVC
}, completion: {
finished in
transitionContext.completeTransition(true)
fromViewController.view.alpha = 1.0
})
}