'how to move NSWindow to a particular screen?
In my app I need to be able to move my app's windows between the screens programmatically. I'm working on my MacBookPro and I'm connected to DELL monitor. So what I want to do is to have a method that would move my app's window from my laptop screen to the external DELL one.
Does anyone know how to achieve it?
Any help is highly appreciated!
Solution 1:[1]
[NSScreen screens]
gives you an array of NSScreens. The screen at index 0 is the one that's got your menu on.
So pick the other screen from the array, find it's visibleFrame
and change the frame of your window to go inside it.
Solution 2:[2]
Following Steve Waddicor's comment, I tried putting the following code in override func viewDidLoad(){}
and I was able to display a window on the main screen, on my laptop.
By changing screens[0]
to screens[1]
, your window should be on your external display.
I hope this helps somebody.
DispatchQueue.main.async {
//set up the main display as the display where window shows up
let screens = NSScreen.screens
var pos = NSPoint()
pos.x = screens[0].visibleFrame.midX
pos.y = screens[0].visibleFrame.midY
self.view.window?.setFrameOrigin(pos)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Julian F. Weinert |
Solution 2 | user8460166 |