NSWindow contentView不能覆盖整个窗口大小-macOS& SwiftUI [英] NSWindow contentView not cover full window size - macOS & SwiftUI
本文介绍了NSWindow contentView不能覆盖整个窗口大小-macOS& SwiftUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用SwiftUI启动一个新的macOS应用程序,但是我遇到了一个大问题.
该应用程序需要全尺寸contentView
(在titleBar
下方),但我无法完成.在一个新项目中,使用Storyboards可以正常工作,但使用SwiftUI则不能.
I'm starting a new macOS app with SwiftUI but I have a big problem.
The app needs a full size contentView
(underneath titleBar
) but I can't accomplish. On a new project using Storyboards works fine, but with SwiftUI not.
我的代码:
结果:
它应该看起来像这样:
And it should look like this:
有什么想法吗? 谢谢!
Any ideas? Thanks!
推荐答案
我刚刚在AppDelegate
中使用了以下变体,ContentView
的内容可以是其他任何内容
I just used the following variant in AppDelegate
, the content of ContentView
and others can be any
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)
}
这篇关于NSWindow contentView不能覆盖整个窗口大小-macOS& SwiftUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文