willTransition not triggered when flipping device in landscape mode

Hello,


The following function is triggered whenever the device is rotated, except in the case when I "flip" the device along it's X axis when in landscape mode.


override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {

print("orientation after activity = " + String(UIDevice.current.orientation.rawValue))

}


If I hold a device in landscape mode, and then rotate it away from me, so that the screen is facing away, the view rotates, but the willTransition func is not triggered. So if I am starting out in landscape left with the device facing me, and then do my "flip", it's essentially now landscape right with the screen facing away from me.


I am building a "flip mode" in my app where I want two specific views to rotate, but I don't want the entire window to rotate. I can't seem to figure out how to detect this transition so that I can properly handle it in my code.

Have you checked:

- which orientations are authorized in info.plist


You may also have to add this in your viewController



override open var supportedInterfaceOrientations : UIInterfaceOrientationMask     {
        return .all
    }



Note that iPhoneX series (with notch) does not support portrait upside down.

Thanks for replying. I have them all checked, and evern tried to override the supported interface orientations as you suggested. The window rotates, but I don't get an event in willTransition.

You wrote:

If I hold a device in landscape mode, and then rotate it away from me, so that the screen is facing away, the view rotates,…


what is the end configuration: portrait upside or downside ?

Don't you ever get a print, whatever rotation ? (from landscape to portrait, from portrait to landscape) ?


Did you notice:

If you override this method in your own objects, always call super at some point in your implementation so that UIKit can forward the trait changes to the associated presentation controller and to any child view controllers. View controllers forward the trait change message to their child view controllers. Presentation controllers forward the trait change to their presented view controller.

I do get other print statements, just nothing from when I do the "flip" from landscape left facing me to screen facing away from me (which is landscape right if you were looking at it from opposite me).


I'll make sure to handle super as well.

>is triggered whenever the device is rotated


Sorry if I missed it - which device (not simulator)?


In what orientation is the app designed to launch? In what orientation examples are you in when the app is launched?

I am facing the same problem.

A navigation app needs to update the headingOrientation property on CLLocationManager to get heading updates relative to the user interface orientation. So I wanted to use [UIViewController viewWillTransitionToSize:withTransitionCoordinator:] to detect user interface orientation changes, but this method is not called when the device if flipped from landscape left to right via upside-down (which is not supported and therefore not reported).

Alternatives:

  • UIDeviceOrientationDidChangeNotification's may not be in sync with UIKit when the user has the orientation lock activated
  • [UIViewController didRotateFromInterfaceOrientation:] is deprecated

EDIT: The solution is to use [UIViewController viewWillTransitionToSize:withTransitionCoordinator.] which is called even though the size is not changed.

Use:

func application(_ application: UIApplication, willChangeStatusBarOrientation newStatusBarOrientation: UIInterfaceOrientation, duration: TimeInterval) { ... }

func application(_ application: UIApplication, didChangeStatusBarOrientation oldStatusBarOrientation: UIInterfaceOrientation) { ... }

They get triggered...

willTransition not triggered when flipping device in landscape mode
 
 
Q