iOS 8 - 关闭带有自定义演示文稿的视图控制器后屏幕空白

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25588617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:06:57  来源:igfitidea点击:

iOS 8 - Screen blank after dismissing view controller with custom presentation

iosobjective-ccocoa-touchuikitios8

提问by jaggedcow

When dismissing various view controllers using UIModalPresentationCustom, the screen turns black after the view controller is dismissed, as if all the view controllers had been removed from the view hierarchy.

当使用 解除各种视图控制器时UIModalPresentationCustom,视图控制器被解除后屏幕变黑,好像所有视图控制器都已从视图层次结构中删除。

The transitioning delegate is set properly, the animationControllerForPresentedController is asked for and passed correctly, and the transition is completed once the animation is over.

过渡委托设置正确,animationControllerForPresentedController 被请求并正确传递,一旦动画结束过渡就完成了。

This exact code works perfectly when compiled with the iOS 7 SDK, but is broken when compiled with iOS 8b5

这个确切的代码在使用 iOS 7 SDK 编译时可以完美运行,但在使用 iOS 8b5 编译时会损坏

回答by Scott Kaiser

This is because you are most likely adding both the presenting

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

[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]

[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]

and the presented

和提出的

[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]

[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]

view controllers to your containerView in the (void)animateTransition:(id )transitionContext method of your animation controller. Since you are using a custom modal presentation, the presenting view controlleris still shown beneath the presented view controller. Now since it's still visible you don't need to add it to the container view. Instead only add the presented view controllerto the containerView. Should look something like this inside of your animateTransition: method

在动画控制器的 (void)animateTransition:(id)transitionContext 方法中查看控制器到您的 containerView。由于您使用的是自定义模式呈现,呈现视图控制器仍显示在呈现的视图控制器下方。现在,由于它仍然可见,您无需将其添加到容器视图中。而是只将呈现的视图控制器添加到 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
}

回答by Lcsky

This is the kind of question that the high-votes & accepted answer mislead people. Long words short.

这是高票和接受的答案误导人们的那种问题。长话短说。

Firstly, don't use UIModalPresentationCustom, it's not what it sounds like. (detail)

首先,不要使用 UIModalPresentationCustom,它听起来不像。(详细

Secondly, there is a new method to retrieve from/to Views in animateTransition, don't use something like 'fromVC.view' anymore. (why)

其次,在 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)

Now the black screen should go away.

现在黑屏应该消失了。

回答by billbai

It seems I encountered the same issue, I'm using Xcode 6 beta5.

我似乎遇到了同样的问题,我使用的是 Xcode 6 beta5。

I searched with Google and found someone else has this same issue, and they said this is serious a bug in iOS 8, so hope Apple can fix this soon.

我用谷歌搜索,发现其他人也有同样的问题,他们说这是 iOS 8 中的一个严重错误,所以希望 Apple 能尽快解决这个问题。

https://github.com/TeehanLax/UIViewController-Transitions-Example/issues/5

https://github.com/TeehanLax/UIViewController-Transitions-Example/issues/5

回答by devgeek

I added the code below to the transition completion block and it fixed it for me.

我将下面的代码添加到转换完成块中,它为我修复了它。

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

回答by Gautam Jain

Perhaps the view hierarchy is buggy with new Xcode or maybe it's a little different is iOS8. This code worked for me. Add it while dismissing the controller in animateTransition: transitionContext method.

也许新 Xcode 的视图层次结构有问题,或者 iOS8 有点不同。这段代码对我有用。在 animateTransition: transitionContext 方法中关闭控制器时添加它。

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

回答by smaura777

Quick Tip: Make sure your "From" UIViewController is what you're expecting.

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

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

Had a similar bug in my code in the past. You can easily pull out the right "From" VC context.

过去在我的代码中有类似的错误。您可以轻松地提取正确的“来自”VC 上下文。

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

回答by eliasbagley

I had the same problem, and what caused the problem for me is that I wasn't setting the toViewController's final frame. See the following example from http://www.appcoda.com/custom-view-controller-transitions-tutorial/

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