使用Swift在一个ViewController中强制横向模式

我正在试图强制在我的应用程序中的横向模式只有一个视图,我打电话

override func shouldAutorotate() -> Bool { print("shouldAutorotate") return false } override func supportedInterfaceOrientations() -> Int { print("supportedInterfaceOrientations") return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue) } override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation { return UIInterfaceOrientation.LandscapeLeft } 

视图以纵向模式启动,并在更改设备方向时保持旋转。
shouldAutorotate永远不会被调用。
任何帮助,将不胜感激。

这可能对其他人有用,我发现了一种强制视图以横向模式启动的方法:

把它放在viewDidLoad()中:

 let value = UIInterfaceOrientation.LandscapeLeft.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation") 

和,

 override func shouldAutorotate() -> Bool { return true } 

Swift 3

 override func viewDidLoad() { super.viewDidLoad() let value = UIInterfaceOrientation.landscapeLeft.rawValue UIDevice.current.setValue(value, forKey: "orientation") } private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.landscapeLeft } private func shouldAutorotate() -> Bool { return true } 

使用Swift 2.2

尝试:

 let value = UIInterfaceOrientation.LandscapeLeft.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation") 

其次是:

 UIViewController.attemptRotationToDeviceOrientation() 

从苹果的UIViewController类参考:

某些视图控制器可能想要使用应用程序特定的条件来确定支持的接口方向。 如果你的视图控制器这样做,当这些条件改变时,你的应用程序应该调用这个类的方法。 系统立即尝试旋转到新的方向。

然后,像其他人所build议的那样,根据需要重写以下方法:

 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.LandscapeLeft } override func shouldAutorotate() -> Bool { return true } 

我有一个类似的问题,签名的意见,这解决了我。

Swift 4 ,在iOS 11中testing

您可以在projectTarget – > General – > DeploymentInfo(设备方向) – >纵向 (Landscapeleft和Landscaperight是可选的)中指定方向。

AppDelegate中

 var orientationLock = UIInterfaceOrientationMask.portrait var myOrientation: UIInterfaceOrientationMask = .portrait func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return myOrientation } 

LandScpaeViewController

 override func viewDidLoad() { super.viewDidLoad() let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.myOrientation = .landscape } 

OnDismissButtonTap

 let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.myOrientation = .portrait 

而已。 🙂

在Swift 2.2中工作

  func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if self.window?.rootViewController?.presentedViewController is SignatureViewController { let secondController = self.window!.rootViewController!.presentedViewController as! SignatureViewController if secondController.isPresented { return UIInterfaceOrientationMask.LandscapeLeft; } else { return UIInterfaceOrientationMask.Portrait; } } else { return UIInterfaceOrientationMask.Portrait; } } 

Swift 3.每次用户重新打开应用程序时,都会locking方向。

 class MyViewController: UIViewController { ... override func viewDidLoad() { super.viewDidLoad() // Receive notification when app is brought to foreground NotificationCenter.default.addObserver(self, selector: #selector(self.onDidBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil) } // Handle notification func onDidBecomeActive() { setOrientationLandscape() } // Change orientation to landscape private func setOrientationLandscape() { if !UIDevice.current.orientation.isLandscape { let value = UIInterfaceOrientation.landscapeLeft.rawValue UIDevice.current.setValue(value, forKey:"orientation") UIViewController.attemptRotationToDeviceOrientation() } } // Only allow landscape left override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return UIInterfaceOrientationMask.landscapeLeft } /* // Allow rotation - this seems unnecessary private func shouldAutoRotate() -> Bool { return true } */ ... } 

我需要强制一个控制器纵向。 添加这对我工作。

迅速4与iOS 11

 override var supportedInterfaceOrientations : UIInterfaceOrientationMask{ return .portrait } 

根据supportedInterfaceOrientations的文档, shouldAutorotate方法应该在Objective-C中返回trueYES ,以便考虑supportedInterfaceOrientations

斯威夫特4

试图保持方向没有任何工作,但这对我来说:

 ... override func viewDidLoad() { super.viewDidLoad() forcelandscapeRight() let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #selector(forcelandscapeRight), name: Notification.Name.UIDeviceOrientationDidChange, object: nil) } @objc func forcelandscapeRight() { let value = UIInterfaceOrientation.landscapeRight.rawValue UIDevice.current.setValue(value, forKey: "orientation") } .... 
 class CustomUIViewController : UIViewController{ override var supportedInterfaceOrientations : UIInterfaceOrientationMask{ return .landscapeLeft } } class ViewController: CustomUIViewController { . . . }