I tried the next:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: \"orientation\")
on
swift 5
import UIKit
extension UIViewController {
class func setUIInterfaceOrientation(_ value: UIInterfaceOrientation) {
UIDevice.current.setValue(value.rawValue, forKey: "orientation")
}
}
Swift 4:
Step1:
In Appdelegate - Declare deviceOrientation
var which is portrait
by default
import UIKit
let appDelegate = UIApplication.shared.delegate as! AppDelegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var deviceOrientation = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return deviceOrientation
}
}
Step 2:
In your certain ViewController write this to change orientation -
override func viewWillAppear(_ animated: Bool) {
appDelegate.deviceOrientation = .landscapeLeft
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
You can override UIViewController.supportedInterfaceOrientations()
instead of triggering rotation in viewWillAppear
For Xcode 7
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Landscape
}
For Xcode 6
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
And make sure the desired orientations are enabled in project settings.
Can modify this to implement any combination of orientations.
internal var viewControllerOrientation = 0
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if viewControllerOrientation == 0 {
return .portrait
} else if viewControllerOrientation == 1 {
return .landscapeLeft
} else if viewControllerOrientation == 2 {
return .landscapeRight
}
}
View controller is locked in landscapeLeft orientation
override func viewDidAppear(_ animated: Bool) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.viewControllerOrientation = 1
}
@IBAction func newVCBtnWasPressed(_ sender: Any) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.viewControllerOrientation = 0
// go to different view controller method, such as:
dismiss(animated: true, completion: nil)
}
View controller is locked in landscapeRight orientation
override func viewDidAppear(_ animated: Bool) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.viewControllerOrientation = 2
}
@IBAction func newVCBtnWasPressed(_ sender: Any) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.viewControllerOrientation = 0
// go to different view controller method, such as:
dismiss(animated: true, completion: nil)
}
All other view controllers are locked in portrait orientation. Again, can modify this to implement any combination of locked orientations you want.
To change orientation programatically in swift 3, use the following way:
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
You can use the orientations,
1. portrait 2. portraitUpsideDown 3. landscapeLeft 4. landscapeRight