0

I have a segue, that is triggered via a button that shows another view controller modally over current context. I'm using a custom class to customize the transition, which is this:

class ShowAnimator: NSObject {
}


extension ShowAnimator: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard
        let fromVC = transitionContext.viewController(forKey: .from),
        let toVC = transitionContext.viewController(forKey: .to) else {
            return
    }
    let containerView = transitionContext.containerView
    containerView.insertSubview(fromVC.view, belowSubview: toVC.view)
    let toFrame = transitionContext.finalFrame(for: toVC)
    let screenBounds = UIScreen.main.bounds
    let bottomLeftCorner = CGPoint(x: screenBounds.width, y: 0)
    let finalFrame = CGRect(origin: bottomLeftCorner, size: screenBounds.size)

    UIView.animate(withDuration: transitionDuration(using: transitionContext),
                   animations: {
                    fromVC.view.frame = finalFrame
    },
                   completion: { _ in
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    }
    )
}
}

In my main view controller, I have these relevant functions:

let showAnimator = ShowAnimator()
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destination = segue.destination
    destination.transitioningDelegate = self
}


func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return nil
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return showAnimator
}

Note that this class is a ViewController as well as a UIViewControllerTransitioningDelegate However, whenever I press the button to perform my segue, it does the animation properly, but once it's finished it just shows a black screen, rather than the next ViewController.

LFHS
  • 295
  • 1
  • 2
  • 15
  • 2
    Here's a really good answer: https://stackoverflow.com/questions/25588617/ios-8-screen-blank-after-dismissing-view-controller-with-custom-presentation – CodyMace Jul 13 '18 at 15:14

1 Answers1

6

One problem is this line:

containerView.insertSubview(fromVC.view, belowSubview: toVC.view)

You are inserting the fromVC view. That is wrong. It is already present (though not necessarily in the container).

And you are not inserting the toVC view. That is also wrong. You need to put it into the container.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I added: `containerView.addSubview(toVC.view) containerView.insertSubview(fromVC.view, aboveSubview: toVC.view)` but now when I dismiss the view controller that was presented modally the screen goes black. Can you show me what to do? – LFHS Nov 05 '17 at 23:59
  • Have we fixed the original problem, i.e. the black screen on presentation? – matt Nov 06 '17 at 00:51
  • 1
    Yes, but when I dismiss the ViewController that was presented modally, it shows a black screen – LFHS Nov 06 '17 at 19:57
  • I'm still confused. The problem posed was: "However, whenever I press the button to perform my segue, it does the animation properly, but once it's finished it just shows a black screen, rather than the next ViewController." Did we solve that problem? Are we now showing "the next ViewController"? – matt Nov 06 '17 at 20:12
  • Ok, so let me try to explain what's happening now. When I press the button, it does the animation properly and shows the right ViewController. However, once that is dismissed, it shows a black screen, rather than the original viewcontroller. Maybe it has something to do with `animationController(for: dimissed`? – LFHS Nov 06 '17 at 20:15
  • Okay, but let me emphasize the point I'm asking you about. You posed a particular problem. I tried to solve it. Did I? If so, the case is closed and you now have a _different_ problem. I don't know what you're doing now, and I can't be responsible for that. (Judging by your comment, you didn't read my answer very carefully and you're still doing this wrong, inserting the `fromVC.view` when you are not supposed to.) – matt Nov 06 '17 at 20:38
  • 1
    Yes my problem went away, but it just made a new problem, which means I didn't get a sufficient answer, and I still need help with this issue. From what I understand from your answer, I should do `containerView.addSubview(toVC.view)` (adding toVC into the container), but what else? – LFHS Nov 06 '17 at 20:59
  • Nothing else. Your job is to add the `toVC.view` to the container and that's all. Then just animate it into place. Done. – matt Nov 06 '17 at 22:36