更改MacOS工程的窗口尺寸和位置.
在applicationDidFinishLaunching
方法中,窗口还未激活,mainWindow
并没有值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| func applicationDidFinishLaunching(_ aNotification: Notification) { modifyWindowSize() }
func modifyWindowSize() { guard let screen = NSScreen.main else { print("screen is unavailable") return }
guard let mainWindow = NSApp.windows.first else { print("mainWindow is unavailable") return } let screenFrame = screen.visibleFrame let width = 800.0 let height = 600.0 let x = NSMidX(screenFrame) - width * 0.5 let y = NSMidY(screenFrame) - height * 0.5
let frame = NSMakeRect(x, y, width, height)
mainWindow.setFrame(frame, display: true)
}
|