在NSTitlebarAccessoryViewController中使用SwiftUI时,布局存在问题。
仅在全屏模式下正确放置附件视图。如果不在全屏模式下,则将其向下移动并剪切,如下所示:
我不清楚为什么附件视图在全屏模式下会有所不同。
如果我将NSHostingView
嵌入另一个视图中,则会看到相同的问题。
当我添加普通的NSView
而不是NSHostingView
时,它会按预期工作。
要复制:
applicationDidFinishLaunching
。func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
let vc = NSTitlebarAccessoryViewController()
vc.fullScreenMinHeight = 32
let accessoryView = NSHostingView(rootView: ContentView())
vc.view = accessoryView
vc.view.frame.size = vc.view.fittingSize
window.addTitlebarAccessoryViewController(vc)
}