0
votes

I am designing an universal app and I like to support portrait only for both iPhone and iPad. But I need iPad to support both regular and upside down portrait orientation.

So I set Supported interface orientations for both iPhone and iPad in info.plist as Portrait (top home button) and Portrait (bottom home button).

Following in AppDeleagte.swift

var shouldSupportAllOrientation = false

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if (shouldSupportAllOrientation == true){
        return UIInterfaceOrientationMask.all
    }
    return UIInterfaceOrientationMask.portrait
}

Following in ViewController.swift

override func viewWillAppear(_ animated: Bool) {
        let appdelegate = UIApplication.shared.delegate as! AppDelegate
        appdelegate.shouldSupportAllOrientation = false

    let value = UIInterfaceOrientation.portraitUpsideDown.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}

When I rotate my iPad upside down in simulator, it still remains upside down and does not rotate 180 degrees. Where am I going wrong?

2

2 Answers

0
votes

You didn't mention how you had configured the Deployment Info for the project. By default, the 'Upside Down' option is unchecked for Universal projects, and you will need to select this to get the orientation you need

Xcode Deployment Info

0
votes

I am able to achieve this by doing

Following in AppDeleagte.swift

var shouldSupportAllOrientation = false

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if (shouldSupportAllOrientation == true){
        return UIInterfaceOrientationMask.all
    }
  return [UIInterfaceOrientationMask.portrait, UIInterfaceOrientationMask.portraitUpsideDown]
}

Following in ViewController.swift

override func viewWillAppear(_ animated: Bool) {
        let appdelegate = UIApplication.shared.delegate as! AppDelegate
        appdelegate.shouldSupportAllOrientation = false
}