なんとかするから、なんとかなる

エンジニア関係のことを書きます

iOS How to solve the black screen when a modal view was dismissed with custom animation.

Introduction

This article shows how to solve the black screen when a modal view was dismissed with custom animation.

When I tried to set an present and modal animation when a modal view was appeared and disappeared.

Though the present animation seemed well, a dismiss animation faced on a problem.

After dismissing a modal view, the screen turned to black and not working anymore.

It seemed the rootViewController was removed from the UIWindow.

In this case I wrote the code as below.

  • **ViewController
func transtionView() {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let myVC = storyboard.instantiateViewController(withIdentifier: "MyVC")    
    myVC.transitioningDelegate = self
    myVC.modalTransitionStyle = .cutom // Here
    self.present(myVC, animation: true)
}

extension ViewController: UIViewControllerTransitioningDelegate {
    func animationController(forPresented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return MyPresentAnimation()
    }

    func animationController(forDismissed: UIViewController) ->  UIViewControllerAnimatedTransitioning?
        return MyDismissAnimation()
    }

Solution

Commented out the myVC.modalTransitionStyle = .custom line, it was working fine.

Though I don't know the details of this issues, it seemed that if I set an modal TransitonStyle parameter for .custom.

Maybe I need to implement the delegate function below

func presentationController(forPresented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?

I'm not sure if it is correct or not. I'm going to search and try it.