I tried the next:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
on
viewWillAppear
but it does not work. How can I rotate my screen on viewWillAppear?
UPDATE
I use the next code for locking an orientation:
var shouldRotate = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
}
}
Swift 4.0
private func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.landscapeLeft.rawValue)
}
}
and in my FIRST controller in viewWillAppear I set:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = true
}
in my SECOND controller:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = false
}
so, when I back from SECOND controller to the FIRST:
I rotate it to left - does not rotate, rotate back to right - nothing, rotate to left, again - it rotates.
How can I fix it?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With