0

I have used the following approach to make it feasible, but didn't work

override func viewDidLoad() {
    super.viewDidLoad()
    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeLeft
}

override var shouldAutorotate: Bool {
    return true
}    
Jason J. Nathan
  • 7,422
  • 2
  • 26
  • 37
user7711128
  • 31
  • 1
  • 4

2 Answers2

1

If you want to force Particular viewController rotate to landscape only then add the following code in AppDelega file

public var orientationLock = UIInterfaceOrientationMask.all

    static var shared : AppDelegate {
        return UIApplication.shared.delegate as! AppDelegate
    }


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

    func lockDeviceOrientation(orientationLock: UIInterfaceOrientationMask, rotateOrientation:UIInterfaceOrientation) {
        AppDelegate.shared.orientationLock = orientationLock
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }

And add the following code in particular viewController which you want to force to rotate in landscape only

 override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        AppDelegate.shared.orientationLock = .landscapeLeft
    }
  • It would be better to allow all orientation in the app delegate and just return a `portrait` orientation in the ones you won't want to rotate. – Sulthan May 13 '19 at 12:58
0

In your AppDelegate.swift :

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if let navigationController = self.window?.rootViewController as? UINavigationController {
        if navigationController.visibleViewController is YourViewController {
            return UIInterfaceOrientationMask.all
        } else {
            return UIInterfaceOrientationMask.portrait
        }
    }
    return UIInterfaceOrientationMask.portrait
}
B K.
  • 534
  • 5
  • 18