我刚刚在AppDelegate
中使用了以下变体,ContentView
和其他内容可以是任何内容。
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
.edgesIgnoringSafeArea(.top) // to extend entire content under titlebar
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .texturedBackground, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.titlebarAppearsTransparent = true // as stated
window.titleVisibility = .hidden // no title - all in content
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
}
ContentView
之外的边缘具有不同的效果,这很有趣。这可能是一个错误,或者SwiftUI的组合问题比我想象的更多。是时候进行更多实验了 :) 感谢您的回答! - ingoemContentView
中有一个NavigationView
,那么我仍然需要在NavigationView
上使用edgesIgnoringSafeArea
。 - ingoemwindow.isReleasedWhenClosed = false
来防止在关闭窗口时崩溃。 - johnny peterColor
可以正常工作,但是使用材料/视觉效果时,我必须使用.frame(minWidth: ,minHeight:)
。 - iMaddinedgesIgnoringSafeArea
命令,让您的内容视图忽略安全区域。以下代码类似于您的示例:struct ContentView: View {
var body: some View {
HStack(spacing: 0) {
Text("Hello, World!")
.frame(maxWidth: 200, maxHeight: .infinity)
.background(Color.red)
Text("Hello, World!")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.black)
}.edgesIgnoringSafeArea(.all)
}
}
struct ContentView: View {
var body: some View {
NavigationView {
Text("Hello, World!")
.frame(maxWidth: 200, maxHeight: .infinity)
.background(Color.red)
.edgesIgnoringSafeArea(.all)
Text("Hello, World!")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.black)
.edgesIgnoringSafeArea(.all)
}.edgesIgnoringSafeArea(.all)
}
}
DispatchQueue.main.async {
self.window.orderOut(nil)
self.window.makeKeyAndOrderFront(nil)
}
applicationDidFinishLaunching
中使用window.makeKeyAndOrderFront(nil)
后面接上,这将添加一个非常短的动画效果。如果您觉得它有点令人不舒服,可以通过NSWindow.animationBehavior
或类似的方式关闭它。window.makeKeyAndOrderFront(nil)
并将其替换为上面的调度队列逻辑,则不会出现动画效果。因此,最终您将拥有:func applicationDidFinishLaunching(_ aNotification: Notification) {
let contentView = ContentView()
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView, .texturedBackground],
backing: .buffered, defer: false)
window.titlebarAppearsTransparent = true
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
// window.makeKeyAndOrderFront(self) <- don't call it here
DispatchQueue.main.async {
self.window.orderOut(nil)
self.window.makeKeyAndOrderFront(nil)
}
}
在 SwiftUI 应用程序生命周期方面提供更多信息。
您需要将窗口样式设置为 HiddenTitleBarWindowStyle:
WindowGroup {
ContentView()
}.windowStyle(HiddenTitleBarWindowStyle())
使用纯SwiftUI的最小解决方案。
@main
struct X_App: App {
var body: some Scene {
WindowGroup {
ContentView()
.edgesIgnoringSafeArea(.top)
}.windowStyle(.hiddenTitleBar)
}}