Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center window in screen

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)  
    }
like image 792
SNos Avatar asked Feb 09 '16 11:02

SNos


People also ask

Is there a way to center a window?

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.

How do I center a window in Windows 11?

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.


1 Answers

For future references this is done inside NSWindowController class using self.window?.center()

like image 167
SNos Avatar answered Sep 30 '22 06:09

SNos