To make it more clear I have created a demo app with only 2 ViewControllers. The issue only occurs on iPad on which I have kept all orientations enabled which are already enabled by default. So if I present a UIViewController as FormSheet on iPad, the viewWillTransition method is not triggering sometimes when iPad is in landscape mode. I tested it both in simulator as well as on the physical device. Firstly I thought it might be due to my code but in this demo it was clear, if I try to print a statement in viewWillTransition method in my ViewController, it will be printed every time no matter which orientation you are in. However, when I present a FormSheet, it will work for every orientation if iPad is in Portrait mode but if you switch to landscape mode, and try to change app size in Split View, sometimes the statement is not printed. It also happens if you have 2 apps opened in Split Mode with equal ratio and you switch your app to slideOver. So im confused why viewWillTranstion working fine for portrait mode but sometimes will not trigger for landscape mode?

This is my code in ViewController:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        
    }

    @IBAction func button(_ sender: Any) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        if self.traitCollection.userInterfaceIdiom == .phone {
            vc.modalPresentationStyle = .fullScreen
        }
        else {
            vc.modalPresentationStyle = .formSheet
        }
        vc.modalTransitionStyle = .coverVertical
        self.present(vc, animated: true, completion: nil)
    }
    
}

And this is code for SecondViewController:

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        
        DispatchQueue.main.async {
            print("Transition Detected")
        }
    }

}

Here is a video in which you can see the statement is only getting printed in portrait mode: https://youtu.be/iz5iOzOtGCg

It gets printed in landscape mode too but I have included the part ion which viewWillTransition is not triggering.

In Portrait mode, no matter what you do, rotate, change to Split View or drag size of app in Split View, or switch app to slipover, Transition Detected is printed. However in landscape mode, it will not print if you are in Split View and drag to make app small.

This behaviour is so confusing. Any idea how to detect all the transitions in landscape mode too?

0

There are 0 best solutions below