IOS8-自定义显示视图控制器后屏幕空白

当使用 UIModalPresentationCustom解除各种视图控制器时,在解除视图控制器之后,屏幕会变黑,就好像所有的视图控制器都已经从视图层次结构中删除了一样。

正确设置转换委托,请求 animationControllerForPresentedController 并正确传递,动画结束后完成转换。

当用 iOS7SDK 编译时,这段代码可以很好地工作,但是当用 iOS8b5编译时,这段代码就被破坏了

22379 次浏览

看起来我遇到了同样的问题,我正在使用 Xcode 6 beta 5。

我在谷歌上搜索了一下,发现其他人也有同样的问题,他们说这是 iOS8的一个严重缺陷,所以希望苹果能尽快解决这个问题。

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];
}];

这是因为您很可能同时添加了这两个演示文稿

[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]

还有礼物

[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]

在您的动画控制器的(void) animateTransfer: (id) trantionContext 方法中查看容器控制器。因为您使用的是自定义模态表示,所以 显示视图控制器仍然显示在 提出的视图控制器下面。现在,因为它仍然是可见的,所以不需要将它添加到容器视图中。取而代之的是将 只添加提供的视图控制器转换为容器视图。应该在 animateTransfer: 方法内部看起来像这样

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
}

也许视图层次结构在使用新的 Xcode 时存在缺陷,或者 iOS8有些不同。这个代码对我很有用。添加它,同时在 animateTransfer: trantionContext 方法中忽略控制器。

[[UIApplication sharedApplication].keyWindow addSubview:toViewController.view];
toViewController.view.userInteractionEnabled = YES;

快速提示: 确保您的“ From”UIViewController 是您所期望的。

    NSLog(@" FROM vc %@" , [[transitionContext  viewControllerForKey:UITransitionContextFromViewControllerKey] description]);

过去我的代码中也有类似的 bug,你可以很容易地找到正确的“ From”VC 上下文。

  UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
fromView =  [[[fromVC childViewControllers] firstObject]  view];

这就是那种高票通过的答案误导人们的问题。长话短说。

首先 ,不要使用 UIModalPresentationCustom,它不像听起来那样。(细节)

其次 ,有一个新的方法可以从/to 视图中检索 animateTransfer,不要再使用类似于“ from VC.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)

现在黑屏应该消失了。

我遇到了同样的问题,对我来说,导致这个问题的原因是我没有设置 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
})
}