Display os x window full screen on secondary monitor using Cocoa
-
03-07-2019 - |
Question
I'm working on a Cocoa Mac app where I need to display a window/view on a secondary monitor, full-screen. I know how to create a window that could be dragged onto the secondary monitor, but I was wanting to programatically create the window and make it full screen on the external monitor. Thanks for the help.
Solution
First, determine which screen you want to use by iterating over [NSScreen screens].
Create a full screen window with:
NSScreen *screen = /* from [NSScreen screens] */
NSRect screenRect = [screen frame];
NSWindow *window = [[NSWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
screen:screen];
[window setLevel: CGShieldingWindowLevel()];
You might want to google CGDisplayCapture() as well.
OTHER TIPS
The full screen window animations are choppy and don't look good in my opinion. The fullscreen view is much smoother.
Try this:
- (void)toggleMyViewFullScreen:(id)sender
{
if (myView.inFullScreenMode) {
[myView exitFullScreenModeWithOptions:nil];
} else {
NSApplicationPresentationOptions options =
NSApplicationPresentationHideDock |
NSApplicationPresentationHideMenuBar;
[myView enterFullScreenMode:[NSScreen mainScreen] withOptions:@{
NSFullScreenModeApplicationPresentationOptions : @(options) }];
}];
}
}
You can connect this to the fullscreen menu item in the Window menu (after inserting that into your nib) but be sure to change the action that the menu item fires to your toggleMyViewFullScreen: . Or your can invoke toggleMyViewFullScreen programmatically or when your app loads.