gpt4 book ai didi

ios - 在 MacCatalyst 的 SwiftUI 应用程序中隐藏标题栏

转载 作者:行者123 更新时间:2023-12-04 12:19:21 26 4
gpt4 key购买 nike

如何在新的 SwiftUI 应用协议(protocol)中隐藏标题栏?
由于 AppDelegate.swift 和 SceneDelegate.swift 协议(protocol)消失了,我不能再遵循这个文档了:
https://developer.apple.com/documentation/uikit/mac_catalyst/removing_the_title_bar_in_your_mac_app_built_with_mac_catalyst
我无法实现此代码:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst)
if let titlebar = windowScene.titlebar {
titlebar.titleVisibility = .hidden
titlebar.toolbar = nil
}
#endif
}
}
希望新的 AppProtocol 仍有可能。
提前致谢

最佳答案

这是隐藏标题栏的方法:

struct ContentView: View {
var body: some View {
ZStack {
Text("Example UI")
}
.withHostingWindow { window in
#if targetEnvironment(macCatalyst)
if let titlebar = window?.windowScene?.titlebar {
titlebar.titleVisibility = .hidden
titlebar.toolbar = nil
}
#endif
}
}
}

extension View {
fileprivate func withHostingWindow(_ callback: @escaping (UIWindow?) -> Void) -> some View {
self.background(HostingWindowFinder(callback: callback))
}
}

fileprivate struct HostingWindowFinder: UIViewRepresentable {
var callback: (UIWindow?) -> ()

func makeUIView(context: Context) -> UIView {
let view = UIView()
DispatchQueue.main.async { [weak view] in
self.callback(view?.window)
}
return view
}

func updateUIView(_ uiView: UIView, context: Context) {
}
}

关于ios - 在 MacCatalyst 的 SwiftUI 应用程序中隐藏标题栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65238068/

26 4 0