panthro
panthro

Reputation: 24079

Force Landscape Mode Only

I have the following in my info.plist:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

Yet my app still opens in portrait. How can I force it to be landscape only.

Upvotes: 2

Views: 896

Answers (2)

Coskun Caner
Coskun Caner

Reputation: 121

it is quite simple just create new project and leave the 'Portrait Orientation' check box empty in Project Settings > General tab of your Build Target.

example: like this

this will update your project's 'Info.plist' file like you shared above. It always works for a new project. if it is still not working for you, you probably overriding device orientation somewhere in your code, after your application lunched.

i usually use a Root ViewController, and a Custom UINavigationController on it to maintain an easier navigation model on my projects, here is an example:

...

////////////////////////
// MARK: - Main NavCon
//
class MainNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black // some color for debug
        isNavigationBarHidden = true
    }

    //    override var preferredStatusBarStyle: UIStatusBarStyle {
    //        return .lightContent
    //    }

    // this one locks the device to Landscape orientation from root
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.landscape //.portrait
    }
    override var shouldAutorotate: Bool {
        return true // false, if you want to lock
    }
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeLeft //.portrait
    }
}

...

and, this is how i use it on didLunch:

...

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()

        let rootVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyFirstViewController") as! MyFirstViewController
        let navcon = MainNavigationController(rootViewController: rootVC)
        window?.rootViewController = navcon

        return true
    }

    //... Other Delegates ...
}

...

after that, every ViewController pushed/presented from this navigation stack, will inherit your initial setup in MainNavigationController.

Upvotes: 0

βhargavḯ
βhargavḯ

Reputation: 9836

try adding below in your AppDelegate

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

Upvotes: 1

Related Questions