Reputation: 4107
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!
Upvotes: 6
Views: 4611
Reputation: 183
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)
}
Upvotes: 1
Reputation: 2217
[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.
Upvotes: 11