本文介绍了NSWindow contentView 未覆盖整个窗口大小 - macOS &用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.

我的代码:

结果:

它应该是这样的:

有什么想法吗?谢谢!

推荐答案

我只是在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 &用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 07:32