2

I have an issue about the orientation on iPad. I disabled the orientation on iPhone but i can change it programmatically on some specific controller. But on iPad i can’t. i don’t know why although i searched in this website and some developers write answers to check UIRequiresFullScreene and i do it. But it disables the orientation on all screens. Is there any way to give ability to change orientation on specific controller on iPad ?

The following code i used to change iPad Orientation but it is not working.

UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

Thanks advance

Chris
  • 7,579
  • 3
  • 18
  • 38
Hamza Hassan
  • 107
  • 1
  • 10
  • Does this answer your question? [Force landscape mode in one ViewController using Swift](https://stackoverflow.com/questions/27037839/force-landscape-mode-in-one-viewcontroller-using-swift) – Roman Podymov Jun 11 '20 at 20:40

1 Answers1

1

When you set programmatically the orientation of the device. To perform the orientation animation the function

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask 

from AppDelegate is being called.

If you have set it there to return only UIInterfaceOrientationMask.portrait you will never see the transition.

Also I personally have these kind of problems and to solve this I had to programmaticaly force the phone to rotate to the current Orientation and then to rotate to the wanted orientation.

Example for phone to rotate landscapeLeft from portrait

func setOrientationToLandScapeOnly() {
    appDelegate.shouldForceLandscapeOnly = true
    appDelegate.shouldForcePortraitOnly = false
    if UIDevice.current.userInterfaceIdiom == .phone {
        switch UIApplication.shared.statusBarOrientation {
            case .landscapeLeft, .landscapeRight:
                return
            default:
                APIOrientationHandler.shared.setDeviceValueForOrientation(UIInterfaceOrientation.portrait.rawValue)
                APIOrientationHandler.shared.setDeviceValueForOrientation(UIInterfaceOrientation.landscapeLeft.rawValue)
                return
        }
    }
}

And inside AppDelegate.swift

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if shouldForcePortraitOnly {
        return UIInterfaceOrientationMask.portrait
    }

    if shouldForceLandscapeOnly {
        return UIInterfaceOrientationMask.landscape
    }

    if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
        return UIInterfaceOrientationMask.landscape
    }

    return UIInterfaceOrientationMask.portrait
}
Vasilis D.
  • 1,416
  • 13
  • 21