[英]Set Orientation For Only One View Controller iOS 13 Swift
I'm creating an iPhone-only app using the latest version of Swift and Xcode, with 3 tabBar sections in the app, and in only one tab bar I want the user to have its screen fixed to landscape.我正在使用最新版本的 Swift 和 Xcode 创建一个仅限 iPhone 的应用程序,该应用程序中有 3 个 tabBar 部分,并且我希望用户仅在一个选项卡栏中将其屏幕固定为横向。 I've tried almost all possible solutions I found on StackOverflow and some other websites, all not seeming to work (including this one: How to lock orientation of one view controller to portrait mode only in Swift ).
我已经尝试了我在 StackOverflow 和其他一些网站上找到的几乎所有可能的解决方案,但似乎都没有奏效(包括这个: 如何仅在 Swift 中将一个视图控制器的方向锁定为纵向模式)。 Anyone know why?
有谁知道为什么?
This is the view controller of the view that I want to be fixed in landscape orientation:这是我想在横向固定的视图的视图控制器:
import UIKit
@objcMembers class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
themes()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
//Some Code
///////////////////////////////////////
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var shouldAutorotate: Bool {
return false
}
///////////////////////////////////////
}
You can force application to rotate orientation using below code,您可以使用以下代码强制应用程序旋转方向,
UIDevice.current.setValue(.landscapeRight, forKey: "orientation")
Please implement below delegate method in AppDelegate and return landscape mask when you want application to rotate to landscape mode,请在 AppDelegate 中实现以下委托方法并在您希望应用程序旋转到横向模式时返回横向蒙版,
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.