SwiftUI與macOS開發:macOS窗臺控制(window)--默認尺寸、最小尺寸和顏色

窗口大小的設置主要在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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章