【发布时间】:2020-01-07 17:36:48
【问题描述】:
我有一个 SwiftUI 项目。对于除一个视图之外的所有视图,我想允许纵向和仅纵向模式。只有一个视图,我想同时允许纵向和横向。 Swift 上有一些资源,但我在 SwiftUI 上找不到。
有没有人找到办法做到这一点?
【问题讨论】:
标签: ios orientation swiftui landscape portrait
我有一个 SwiftUI 项目。对于除一个视图之外的所有视图,我想允许纵向和仅纵向模式。只有一个视图,我想同时允许纵向和横向。 Swift 上有一些资源,但我在 SwiftUI 上找不到。
有没有人找到办法做到这一点?
【问题讨论】:
标签: ios orientation swiftui landscape portrait
我不得不做类似的事情。这是我们的方法。
我们将项目方向设置为仅支持纵向模式。
然后在您的AppDelegate 中添加一个实例变量用于方向并符合supportedInterfaceOrientationsFor 委托方法。
static var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
然后,当您要呈现横向视图时,请执行以下操作:
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
在解雇时,
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
希望这会有所帮助!
【讨论】:
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft, forKey: "orientation") 时出现错误,但我真的不需要那部分。第一行就是我所需要的。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__SwiftValue longLongValue]: unrecognized selector sent to instance 0x282de73c0' 有什么想法吗?
对上述答案的一些调整:
在 AppDelegate 作为上面乔纳森的回答:
static var orientationLock =
UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window:
UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
然后在“destinationView”中 - 风景:
import SwiftUI
struct DestinationView: View {
var body: some View {
Group {
Text("Hello")
}.onAppear {
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
.onDisappear {
DispatchQueue.main.async {
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
}
}
}
注意 UIDevice 行中的 .rawValue 消除了“LongValue”错误。此外,在 .onDisappear 中,我必须使用 DispatchQueue.main.async 以避免在返回到纵向视图时出现错误。
【讨论】:
如果您的应用使用 SwiftUI lifeCycle,则应用会使用符合 App 协议的自定义结构启动。您的应用没有实现上述解决方案所需的 AppDelegate。
要制作您自己的 AppDelegate,请在您的 appnameApp.swift 中尝试此代码
@main
struct appnameApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
static var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// something to do
return true
}
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window:UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
}
来源:https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app
【讨论】:
使用来自https://stackoverflow.com/a/41811798/10330141的AppUtility
在 SwiftUI 中
创建自定义 UIHostingController
class OrientationHostingController : UIHostingController<AnyView> {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
AppUtility.lockOrientation(.portrait)
}
}
let rootView = OrientationHostingController(rootView: AnyView(SwiftUIView()))
【讨论】: