3

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.

Community
  • 1
  • 1
MaxKlinge
  • 51
  • 4
  • Try setting the window level before ordering it in. If that doesn't work, try overriding `-constrainFrameRect:toScreen:` to just return the passed-in rect. – Ken Thomases Feb 08 '15 at 06:09
  • Thank you Ken, but i did not help. I found out that it did work if I activated "Visible At Launch" for my NSWindow in the storyboard. If I then programmable ordered the window out, then next time is run the initial script it was wrong again. – MaxKlinge Feb 09 '15 at 17:11

1 Answers1

2

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)];
MaxKlinge
  • 51
  • 4
  • Any chance this has been updated to swift 4+? I'm working on essentially the same thing, but I do not know obj-c in order to update/convert this to swift 4+. – SouthernYankee65 May 13 '21 at 19:21