简体   繁体   English

如何在 SwiftUI 中隐藏标题栏? (适用于 MacOS 应用程序)

[英]How can I hide title bar in SwiftUI? (for MacOS app)

there那里

I know hide title bar in Storyboard.我知道在 Storyboard 中隐藏标题栏。

在此处输入图像描述

But I can't find the way in SwiftUI.但我在 SwiftUI 中找不到路。

I want to hide title bar with control buttons and make floating image view.我想用控制按钮隐藏标题栏并制作浮动图像视图。

Let me know please.请告诉我。

If you know related example, tell me please.如果您知道相关示例,请告诉我。

My little english sorry..我的小英语对不起..

import SwiftUI
@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }.windowStyle(HiddenTitleBarWindowStyle())
    }
}

try HiddenTitleBarWindowStyle()试试HiddenTitleBarWindowStyle()

Removing the Title Bar in Your Mac App Built with Mac Catalyst删除使用 Mac Catalyst 构建的 Mac App 中的标题栏

Display content that fills the entire height of a window by removing the title bar.通过移除标题栏来显示填充 window 整个高度的内容。

By default, Mac apps built with Mac Catalyst display a title bar across the top of their windows.默认情况下,使用 Mac Catalyst 构建的 Mac 应用会在其 windows 的顶部显示一个标题栏。 A horizontal line separates the title bar from the content of the window.一条水平线将标题栏与 window 的内容分开。 Some Mac apps such as Messages and Contacts have no title bar in their main window.某些 Mac 应用程序(例如消息和联系人)在其主 window 中没有标题栏。 Instead, the top of the window shows only the Close, Minimize, and Zoom buttons with no separator between them and the window's content.相反,window 的顶部仅显示关闭、最小化和缩放按钮,它们与窗口内容之间没有分隔符。 In this UI design, the content area fills the entire height of the window.在这个 UI 设计中,内容区域填充了 window 的整个高度。 The following image illustrates these styles in two windows.下图说明了两个 windows 中的这些 styles。 The first window displays a title bar, while the second has none.第一个 window 显示标题栏,而第二个没有。 Screenshot of two windows, one stacked above the other, with a dark background in the content area of each window.两个 windows 的屏幕截图,一个堆叠在另一个之上,每个 window 的内容区域都有深色背景。

Remove the Title Bar If you choose to design your window without a title bar, you must remove it from the window.移除标题栏 如果您选择设计没有标题栏的 window,则必须将其从 window 中移除。 To remove the title bar, set the title bar's titleVisibility property to UITitlebarTitleVisibility.hidden and the toolbar property to nil.要删除标题栏,请将标题栏的 titleVisibility 属性设置为 UITitlebarTitleVisibility.hidden 并将工具栏属性设置为 nil。 The following code shows how to remove the title bar and its separator from the window during the setup of a new scene.以下代码显示了在设置新场景期间如何从 window 中删除标题栏及其分隔符。

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

}

Click here for more information点击这里了解更多信息

I could not find a way to hide the toolbar entirely in SwiftUI.我找不到在 SwiftUI 中完全隐藏工具栏的方法。 But this is a possible workaround.但这是一种可能的解决方法。 You can put this code in your AppDelegate file.您可以将此代码放在您的 AppDelegate 文件中。

func applicationDidFinishLaunching(_ aNotification: Notification) {
    let window = NSApplication.shared.windows.first!
    window.titlebarAppearsTransparent = true
    window.backgroundColor = .white
    window.standardWindowButton(.closeButton)!.isHidden = true
    window.standardWindowButton(.miniaturizeButton)!.isHidden = true
    window.standardWindowButton(.zoomButton)!.isHidden = true
}

Using this code will make it appear that the toolbar is hidden when in reality, it is still there.使用此代码将使工具栏看起来是隐藏的,而实际上它仍然存在。 But the buttons are hidden, and the background is transparent.但是按钮是隐藏的,背景是透明的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM