Is there any way to center a window in the center of the screen in OSX?
I am using the code below but it changes just the size but not the position on screen.
override func viewDidLoad() {
super.viewDidLoad()
let ScreenStart = NSSize(width: (NSScreen.mainScreen()?.frame.width)! / 1.5, height: (NSScreen.mainScreen()?.frame.height)! / 1.5)
self.view.frame.size = ScreenStart
self.view.frame.origin = NSPoint(x: (NSScreen.mainScreen()?.frame.origin.x)!/2, y: (NSScreen.mainScreen()?.frame.height)! / 2)
}
By left-clicking and holding the arrow on that spot, you can drag the window to where ever you want it to be on your screen. Windows will remember the shape and position of the window.
Snap with a keyboard Select the window you want to snap and press the Windows Logo Key + Left Arrow or the Windows Logo Key + Right Arrow to snap the window to the side of the screen where you want it to be.
For future references this is done inside NSWindowController
class using self.window?.center()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With