我想使用SceneDelegate,UIWindowScene,UIWindowSceneDelegate和其他UIScene相关的东西。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow.init(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
let vc = UIStoryboard(name: "FCBottomTabBar", bundle: nil).instantiateInitialViewController()!
window?.rootViewController = vc
window?.makeKeyAndVisible()
}如何动态锁定此窗口的方向?例如,仅用于portrait?动态意味着,在运行时,当用户与某物交互时,方向锁定回所有对象。
例如,对于屏幕A,我只需要锁定为纵向。将屏幕B设置为仅横向。
发布于 2020-06-29 02:16:57
从‘设备方向’开始,把它做成肖像。
在AppDelegate中设置变量'orientation‘并将其值设置为UIInterfaceOrientationMask.portrait
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
...
var orientation = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return orientation
}
}现在,在您想要的ViewController中,写入以下内容以更改方向
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
(UIApplication.shared.delegate as! AppDelegate).orientation = .landscapeLeft
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}希望能对你有所帮助。
发布于 2021-09-19 23:25:57
这个other answer似乎过于复杂,并且使用了一个私有的API。在视图控制器中,要设置支持的方向,只需覆盖supportedInterfaceOrientations即可。
例如,要将其锁定为纵向:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
.portrait
}https://stackoverflow.com/questions/58235527
复制相似问题