SwiftUI强制除一个视图外的所有肖像 [英] SwiftUI Force Portrait On All Except One View
问题描述
我有一个SwiftUI项目.对于除其中一种视图外的所有视图,我想允许使用肖像模式,并且只允许使用肖像模式.对于仅一种视图,我想同时允许纵向和横向. Swift上有一些资源,但是我在SwiftUI上找不到任何资源.
I have a SwiftUI project. For all but one of the views, I want to allow portrait and only portrait mode. For only one view, I want to allow both portrait and landscape. There are some resources on Swift but I couldn't find any on SwiftUI.
有人找到实现这一目标的方法吗?
Did anyone find a way to accomplish this?
推荐答案
我必须做类似的事情.这是我们的方法.
I had to do something similar to this. Here's our approach.
我们将项目方向设置为仅支持纵向模式.
We set the project orientation to only support Portrait mode.
然后在AppDelegate
中添加一个用于定向的实例变量,并符合supportedInterfaceOrientationsFor
委托方法.
Then in your AppDelegate
add an instance variable for orientation and conform to the supportedInterfaceOrientationsFor
delegate method.
static var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
然后,当您要呈现风景视图时,请执行以下操作:
Then when your about to present your landscape view perform the following actions:
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
解雇后,
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
希望这会有所帮助!
这篇关于SwiftUI强制除一个视图外的所有肖像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!