NSTitlebarAccessoryViewController中的NSHostingView布局问题

时间:2020-05-29 20:31:33

标签: macos swiftui appkit nstitlebaraccessoryviewcontroller

在NSTitlebarAccessoryViewController中使用SwiftUI时,布局存在问题。

仅在全屏模式下正确放置附件视图。如果不在全屏模式下,则将其向下移动并剪切,如下所示:

Clipping of accessory view

我不清楚为什么附件视图在全屏模式下会有所不同。

如果我将NSHostingView嵌入另一个视图中,则会看到相同的问题。 当我添加普通的NSView而不是NSHostingView时,它会按预期工作。

要复制:

  1. 创建一个macOS SwiftUI应用项目
  2. 用以下内容替换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)
    }

0 个答案:

没有答案