Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa show NSWindow on a specific screen

In a Mac app how can I open a NSWindow on a specific NSScreen (let's say the second screen)?

This is how I show the window, but it only shows on the main screen

self.windowController = NSStoryboard(name: "Main", bundle: nil).instantiateControllerWithIdentifier("mainWindow") as! NSWindowController
let window = self.windowController.window!
window.makeKeyAndOrderFront(self)

Answers in both Swift and OC are welcome.

like image 569
hklel Avatar asked Oct 31 '25 03:10

hklel


1 Answers

Use the class function 'screens' to get an array of all the screens you have. From the array, pick the screen you want your window to appear on. Use the co-ordinates on that window (which are relative to the main window) to make a rect for your new window like this;

    [self.window setFrame:CGRectMake(pos.x, pos.y, [mywindow frame].size.width     
        , [mywindow frame].size.height) display:YES];

where pos is computed from the array of screens and your selectoin.

like image 60
john elemans Avatar answered Nov 01 '25 21:11

john elemans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!