A recent project requires that most of the pages are portrait and only two are landscape, which requires forced rotation of the screen. I checked the information on the Internet, there are many copycat, but few of them are really effective, so I simply summarize one by myself, see the demo at the bottom, all the codes and Settings in the article are from the demo.
The OC version is here
Step 1: Set the project property to portrait only
Note that iPad needs to be set in info.plist:
Step 2: Code in the AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var isForceLandscape: Bool = false var isForcePortrait: Bool = false func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { window = UIWindow.init(frame: UIScreen.main.bounds) window? .backgroundColor = UIColor.white window? .rootViewController = UINavigationController.init(rootViewController: ViewController.init()) window? .makeKeyAndVisible() return true } func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) - > UIInterfaceOrientationMask {the if (isForceLandscape) {/ / this setting allows the type of landscape return. LandscapeRight; }else if (isForcePortrait){ return .portrait; } return .portrait; }}Copy the code
Step 3: The code in the controller, demo is written in the controller’s base class BaseViewController
class BaseViewController: UIViewController {override func viewDidLoad () {. Super viewDidLoad ()} / / / forced landscape func forceOrientationLandscape () {let appdelegate = UIApplication.shared.delegate as! AppDelegate appdelegate.isForceLandscape = true appdelegate.isForcePortrait = false Appdelegate. Application (UIApplication. Shared supportedInterfaceOrientationsFor: self. The Windows) / / forced to flip the screen, Home button on the right. UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "Orientation") / / refresh UIViewController. AttemptRotationToDeviceOrientation ()} / / / forced vertical screen func forceOrientationPortrait () { let appdelegate = UIApplication.shared.delegate as! AppDelegate appdelegate.isForceLandscape = false appdelegate.isForcePortrait = true Appdelegate. Application (UIApplication. Shared supportedInterfaceOrientationsFor: self. The Windows) / / forced to flip the screen, Home button on the right. UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "Orientation") / / refresh UIViewController. AttemptRotationToDeviceOrientation ()}}Copy the code
TestViewController in landscape mode, ViewController in portrait mode:
Use forced landscape in TestViewController:
Override func viewWillAppear (_ animated: Bool) {super. ViewWillAppear (animated) / / forced landscape forceOrientationLandscape ()}Copy the code
Use forced portrait in ViewController:
Override func viewWillAppear(_ animated: Bool) {super.viewwillAppear (animated) // forceOrientationPortrait()}Copy the code
Demo address:Github.com/zmfflying/Z…
Reference data: www.jianshu.com/p/5c773628c…