0

I'm creating an iPhone-only app using the latest version of Swift and Xcode, with 3 tabBar sections in the app, and in only one tab bar I want the user to have its screen fixed to landscape. I've tried almost all possible solutions I found on StackOverflow and some other websites, all not seeming to work(including this one: How to lock orientation of one view controller to portrait mode only in Swift). Anyone know why?

This is the view controller of the view that I want to be fixed in landscape orientation:


import UIKit
@objcMembers class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        }

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

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    //Some Code

///////////////////////////////////////

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

    override var shouldAutorotate: Bool {
        return false
    }
///////////////////////////////////////
}

CYr
  • 349
  • 4
  • 17
  • 2
    Does this answer your question? [How to lock orientation of one view controller to portrait mode only in Swift](https://stackoverflow.com/questions/28938660/how-to-lock-orientation-of-one-view-controller-to-portrait-mode-only-in-swift) – shim Jan 13 '20 at 22:46

1 Answers1

3

You can force application to rotate orientation using below code,

UIDevice.current.setValue(.landscapeRight, forKey: "orientation")

Please implement below delegate method in AppDelegate and return landscape mask when you want application to rotate to landscape mode,

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
shim
  • 9,289
  • 12
  • 69
  • 108