macos - NSWindow contentView 不覆盖整个窗口大小 - macOS 和 SwiftUI
问题描述
我正在使用 SwiftUI 启动一个新的 macOS 应用程序,但我遇到了一个大问题。该应用程序需要全尺寸contentView
(下方titleBar
),但我无法完成。在使用 Storyboards 的新项目上工作正常,但使用 SwiftUI 则不行。
有任何想法吗?谢谢!
解决方案
我只是在中使用了以下变体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)
}
推荐阅读
- java - 如何获取 JSON 中的第一个键值?
- android - 颤振驱动程序和集成测试包不适用于特定的颤振版本和特定的包。(颤振驱动程序依赖问题)
- c++ - 为什么在使用多线程时什么都没有打印
- javascript - 如何使用 Outlook 加载项将电子邮件正文的强制类型从文本更改为 html,我正在使用 Office js 从加载项访问电子邮件正文
- c# - 如何引用面板中的标签?
- sql-server - 左连接属性上的 EF Core 排序非常慢(但生成的 SQL 很快)
- python - Python 矩阵列表
- websphere - 一次链接多个模块 - IBM DOORS
- python - Tkinter 登录页面无法正常运行
- java - Spring Boot POST 请求返回 http 状态 405“方法不允许”而不是 HTTP 状态 404