ios 使用 Swift 在一个 ViewController 中强制横向模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27037839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force landscape mode in one ViewController using Swift
提问by sazz
I am trying to force only one view in my application on landscape mode, I am calling
我试图在横向模式下在我的应用程序中只强制一个视图,我正在调用
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
}
The view is launched in the portrait mode, and keep rotating when I change the device orientation.
The shouldAutorotate is never called.
Any help would be appreciated.
视图以纵向模式启动,并在我更改设备方向时保持旋转。
shouldAutorotate 永远不会被调用。
任何帮助,将不胜感激。
回答by sazz
It may be useful for others, I found a way to force the view to launch in landscape mode:
它可能对其他人有用,我找到了一种强制视图以横向模式启动的方法:
Put this in the viewDidLoad():
把它放在 viewDidLoad() 中:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
and,
和,
override var shouldAutorotate: Bool {
return true
}
回答by Manee ios
Swift 4
斯威夫特 4
override func viewDidLoad() {
super.viewDidLoad()
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override var shouldAutorotate: Bool {
return true
}
If your view is embedded in a navigation controller, the above alone won't work. You have to cascade up by the following extension after the class definition.
如果您的视图嵌入在导航控制器中,则仅靠上述方法是行不通的。您必须在类定义之后通过以下扩展级联。
extension UINavigationController {
override open var shouldAutorotate: Bool {
get {
if let visibleVC = visibleViewController {
return visibleVC.shouldAutorotate
}
return super.shouldAutorotate
}
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
get {
if let visibleVC = visibleViewController {
return visibleVC.preferredInterfaceOrientationForPresentation
}
return super.preferredInterfaceOrientationForPresentation
}
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
if let visibleVC = visibleViewController {
return visibleVC.supportedInterfaceOrientations
}
return super.supportedInterfaceOrientations
}
}}
Swift 3
斯威夫特 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
}
回答by Zeesha
Swift 4, Tested in iOS 11
Swift 4,在iOS 11 中测试
You can specify the orientation in projectTarget -> General -> DeploymentInfo(Device Orientation) -> Portrait(Landscapeleft and Landscaperight are optional)
您可以在projectTarget -> General -> DeploymentInfo(Device Orientation) -> Portrait 中指定方向(Landscapeleft 和 Landscaperight 是可选的)
AppDelegate
应用程序委托
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
OnDismissButtonTap
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myOrientation = .portrait
Thats it. :)
就是这样。:)
回答by Aidan Malone
Using Swift 2.2
使用 Swift 2.2
Try:
尝试:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
Followed By:
其次是:
UIViewController.attemptRotationToDeviceOrientation()
From Apple's UIViewController Class Reference:
来自 Apple 的 UIViewController 类参考:
Some view controllers may want to use app-specific conditions to determine what interface orientations are supported. If your view controller does this, when those conditions change, your app should call this class method. The system immediately attempts to rotate to the new orientation.
某些视图控制器可能希望使用特定于应用程序的条件来确定支持哪些界面方向。如果您的视图控制器这样做,当这些条件发生变化时,您的应用程序应调用此类方法。系统立即尝试旋转到新方向。
Then, as others have suggested, override the following methods as appropriate:
然后,正如其他人所建议的那样,酌情覆盖以下方法:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.LandscapeLeft
}
override func shouldAutorotate() -> Bool {
return true
}
I was having a similar issue with a signature view and this solved it for me.
我在签名视图方面遇到了类似的问题,这为我解决了这个问题。
回答by Microbob
For me, the best results came from combining Zeesha's answer and sazz's answer.
对我来说,最好的结果来自结合 Zeesha 的回答和 sazz 的回答。
Add the following lines to AppDelegate.swift:
将以下行添加到 AppDelegate.swift 中:
var orientationLock = UIInterfaceOrientationMask.portrait
var myOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return myOrientation
}
Add the following line to your view controller class:
将以下行添加到您的视图控制器类:
let appDel = UIApplication.shared.delegate as! AppDelegate
Add the following lines to your view controller's viewDidLoad()
:
将以下行添加到您的视图控制器viewDidLoad()
:
appDel.myOrientation = .landscape
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
(optional) Add this line to your dismiss function:
(可选)将此行添加到您的关闭功能中:
appDel.myOrientation = .portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
What these lines of code do is set the default orientation to portrait, rotate it landscape when the view controller loads, and then finally reset it back to portrait once the view controller closes.
这些代码行的作用是将默认方向设置为纵向,在视图控制器加载时将其横向旋转,然后在视图控制器关闭后将其重新设置回纵向。
回答by Rob Schlackman
I needed to force one controller into portrait orientation. Adding this worked for me.
我需要强制一个控制器进入纵向。添加这个对我有用。
swift 4 with iOS 11
使用 iOS 11 快速 4
override var supportedInterfaceOrientations : UIInterfaceOrientationMask{
return .portrait
}
回答by Elangovan
In AppDelegate add this:
在 AppDelegate 中添加:
//Orientation Variables
var myOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return myOrientation
}
Add this in viewController, that want to change orientation:
在想要改变方向的 viewController 中添加这个:
override func viewDidLoad() {
super.viewDidLoad()
self.rotateToLandsScapeDevice()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.rotateToPotraitScapeDevice()
}
func rotateToLandsScapeDevice(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myOrientation = .landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UIView.setAnimationsEnabled(true)
}
func rotateToPotraitScapeDevice(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myOrientation = .portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIView.setAnimationsEnabled(true)
}
回答by Daxito
Swift 4
斯威夫特 4
Trying to keep the orientation nothing worked but this for me:
试图保持方向没有任何效果,但这对我来说:
...
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")
}
....
回答by Greg T
Swift 3. This locks the orientation each time the user re-opens the app.
Swift 3. 每次用户重新打开应用程序时都会锁定方向。
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
}
*/
...
}
回答by idris y?ld?z
Works in Swift 2.2
适用于 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;
}
}