MaxKlinge
MaxKlinge

Reputation: 61

Display NSWindow on secondary screen

I would like to display an NSWindow created in a storyboard full screen on a secondary monitor. The below code results in the window being displayed on the primary screen/main screen. The Y origin is ok but the X origin is 0 where it should be -1680. The code below worked before Yosemite.

NSScreen *screen = [[NSScreen screens] objectAtIndex:2];
NSRect mainDisplayRect = [screen frame];
[PresenterWindow setFrame: mainDisplayRect display:YES animate:YES];
[PresenterWindow makeKeyAndOrderFront:sender];
[PresenterWindow setLevel: CGShieldingWindowLevel()];

I had also tried the following with the same result:

[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, [screen frame].size.width, [screen frame].size.height) 
                            styleMask:NSBorderlessWindowMask
                              backing:NSBackingStoreBuffered 
                                defer:NO
                               screen:screen];

Please advise how I can fix this.

I've tried solutions from some other questions:

I have some new observations:

If i activate the "Launch At Startup" in the storyboard for my NSWindow the window is viewed fullscreen on my secondary monitor. If i then do a orderOut and run the above code the window is viewed in correct size but on my primary/main screen. The window is not activated to "Release When Close". So when I deactivate "Launch At Startup" the window is again viewed in the primary/main screen with the size of the secondary screen.

Upvotes: 3

Views: 2538

Answers (1)

MaxKlinge
MaxKlinge

Reputation: 61

After some days I got it to work:

The NSWindow in the storyboard was marked with "Launch At Startup"

In the applcationDidFinishLaunching i wrote the following:

[PresenterWindow setLevel: NSNormalWindowLevel];
[PresenterWindow orderOut:self];

To open full screen I used this:

NSScreen *screen = [[NSScreen screens] objectAtIndex:1];
NSRect mainDisplayRect = [screen frame];
[PresenterWindow setFrame: mainDisplayRect display:YES animate:YES];
[PresenterWindow setLevel: CGShieldingWindowLevel()];
[PresenterWindow makeKeyAndOrderFront:screen];

To close full screen and to be able to open full screen again correctly:

[PresenterWindow setLevel: NSNormalWindowLevel];
[PresenterWindow orderOut:(sender)];

Upvotes: 3

Related Questions