In swift 5, as in the previous case, if you want to allow (avoid others) the rotation of a specific UIViewController in a specific orientation, you must override "supportInterfaceOrientations" inside your UIViewController class as follows:
class MyViewController:UIViewController{
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get{
return .portrait
}
}
}
There are 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 distinguish between iPad or iPhone, you can use UIUserInterfaceIdiom:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get{
return UIDevice.current.userInterfaceIdiom == .phone ? [.portrait, . portraitUpsideDown]:.all
}
}
Where:
public enum UIUserInterfaceIdiom : Int {
case unspecified
@available(iOS 3.2, *)
case phone
@available(iOS 3.2, *)
case pad
@available(iOS 9.0, *)
case tv
@available(iOS 9.0, *)
case carPlay
}
source
share