I have an UIViewController, I want to disable or enable rotation of the screen in different scenarios
Example:
if flag {
rotateDevice = false
}
e
swift 5
override var shouldAutorotate: Bool {
false
}
An easy solution for the most common request (iPhone: portrait, iPad: all) in your AppDelegate
:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
}
swift 4
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return .portrait
}
}
I have the answer.
On AppDelegate
, if you rotate device, push viewcontroller, etc. This function always call
Update for swift 3/4
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
return self.restrictRotation
}
self.restrictRotation
is a custom parameter.
How to use:
In Appdelegate:
var restrictRotation:UIInterfaceOrientationMask = .portrait
In ViewController:
When method ViewDidLoad or viewWillAppear is called. We will change like this:
(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all
and then this method on AppDelegate will be called.
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
In swift 5, as from previous, if you want to allow (avoid others) a particular UIViewController to rotate in a certain orientation you have to override "supportedInterfaceOrientations" inside your UIViewController class like so:
class MyViewController:UIViewController{
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get{
return .portrait
}
}
}
Here there are the possible options:
public struct UIInterfaceOrientationMask : OptionSet {
public init(rawValue: UInt)
public static var portrait: UIInterfaceOrientationMask { get }
public static var landscapeLeft: UIInterfaceOrientationMask { get }
public static var landscapeRight: UIInterfaceOrientationMask { get }
public static var portraitUpsideDown: UIInterfaceOrientationMask { get }
public static var landscape: UIInterfaceOrientationMask { get }
public static var all: UIInterfaceOrientationMask { get }
public static var allButUpsideDown: UIInterfaceOrientationMask { get }
}
Extended
If you want to be able to distinguish between iPad or iPhone you could use UIUserInterfaceIdiom:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get{
return UIDevice.current.userInterfaceIdiom == .phone ? [.portrait, . portraitUpsideDown]:.all //OBS -> You can also return an array
}
}
where:
public enum UIUserInterfaceIdiom : Int {
case unspecified
@available(iOS 3.2, *)
case phone // iPhone and iPod touch style UI
@available(iOS 3.2, *)
case pad // iPad style UI
@available(iOS 9.0, *)
case tv // Apple TV style UI
@available(iOS 9.0, *)
case carPlay // CarPlay style UI
}
Found this to be by far the simplest implementation, and adaptable to use on specific view controllers. Add this to viewController class, after viewDidLoad()
override open var shouldAutorotate: Bool {
return false
}