我能够在Mac Catalyst中创建一个统一的工具栏,SceneDelegate.swift
:
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
// hide the titlebar
windowScene.titlebar?.toolbar = NSToolbar()
windowScene.titlebar?.titleVisibility = .hidden
...
}
但我想让工具栏像本例中那样透明:https://lukakerr.github.io/swift/nswindow-styles#11-不带分隔符的透明工具栏
这在Mac Catalyst中可能吗?
是的,这在Mac Catalyst中是可能的。在SceneDelegate.swift文件中,将工具栏和标题可见性分别设置为false
和.hidden
。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst)
windowScene.titlebar?.toolbar?.isVisible = false
windowScene.titlebar?.titleVisibility = .hidden
#endif
}
覆盖函数视图DidAppear(_animated:Bool({super.viewDidPear(动画(
#if targetEnvironment(macCatalyst)
if let titlebar = self.view.window?.windowScene?.titlebar {
titlebar.titleVisibility = .hidden
titlebar.toolbar = nil
}
#endif
}
这是一个类似的解决方案:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst) //check target
if let titlebar = windowScene.titlebar {
titlebar.titleVisibility = .hidden
titlebar.toolbar = nil
}
#endif
}