窗口大小的設置主要在AppDelegate.swift中配置
設置默認窗口大小
通過windows.setFrame方法設置窗口大小
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
// Add `@Environment(\.managedObjectContext)` in the views that will need the context.
let contentView = ContentView().environment(\.managedObjectContext, persistentContainer.viewContext).frame(minWidth: 450, maxWidth: .infinity, minHeight: 400, maxHeight: .infinity)
// 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.setFrame(CGRect(x: 10, y: 10, width: 550, height: 650), display: true)
window.center()
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
}
設置窗口最小尺寸
macos中,窗體的大小是由內容的大小決定的,所以我們只需設置內容的最小尺寸,就相當於設置了窗口的最小尺寸
let contentView = ContentView().environment(\.managedObjectContext, persistentContainer.viewContext).frame(minWidth: 450, maxWidth: .infinity, minHeight: 400, maxHeight: .infinity)
設置窗口顏色
window.backgroundColor = NSColor.white