How do I create a Cocoa window programmatically?

Objective CCocoaNswindow

Objective C Problem Overview


My Cocoa app needs some small dynamically generated windows. How can I programmatically create Cocoa windows at runtime?

This is my non-working attempt so far. I see no result whatsoever.

NSRect frame = NSMakeRect(0, 0, 200, 200);
NSUInteger styleMask =    NSBorderlessWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];

NSWindow * window =  [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreRetained    defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window display];

Objective C Solutions


Solution 1 - Objective C

The problem is that you don't want to call display, you want to call either makeKeyAndOrderFront or orderFront depending on whether or not you want the window to become the key window. You should also probably use NSBackingStoreBuffered.

This code will create your borderless, blue window at the bottom left of the screen:

NSRect frame = NSMakeRect(0, 0, 200, 200);
NSWindow* window  = [[[NSWindow alloc] initWithContentRect:frame
					styleMask:NSBorderlessWindowMask
					backing:NSBackingStoreBuffered
					defer:NO] autorelease];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront:NSApp];

//Don't forget to assign window to a strong/retaining property!
//Under ARC, not doing so will cause it to disappear immediately;
//  without ARC, the window will be leaked.

You can make the sender for makeKeyAndOrderFront or orderFront whatever is appropriate for your situation.

Solution 2 - Objective C

A side note, if you want to programatically instantiate the application without a main nib, in the main.m file / you can instantiate the AppDelegate as below. Then in your apps Supporting Files / YourApp.plist Main nib base file / MainWindow.xib delete this entry. Then use Jason Coco's approach to attach the window in your AppDelegates init method.

#import "AppDelegate.h":

int main(int argc, char *argv[])
{

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  [NSApplication sharedApplication];

  AppDelegate *appDelegate = [[AppDelegate alloc] init];
  [NSApp setDelegate:appDelegate];
  [NSApp run];
  [pool release];
  return 0;
}

Solution 3 - Objective C

Try

[window makeKeyAndOrderFront:self]; 

instead of

[window display];

Is that what you're aiming for?

Solution 4 - Objective C

Translating the top rated answer to modern swift (5) gives you something akin to this:

var mainWindow: NSWindow!

...

mainWindow = NSWindow(
    contentRect: NSMakeRect(0, 0, 200, 200),
    styleMask: [.titled, .resizable, .miniaturizable, .closable],
    backing: .buffered,
    defer: false)
mainWindow.backgroundColor = .blue
mainWindow.makeKeyAndOrderFront(mainWindow)

Solution 5 - Objective C

This is what I've come up with myself:

NSRect frame = NSMakeRect(100, 100, 200, 200);
NSUInteger styleMask =    NSBorderlessWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
NSWindow * window =  [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreBuffered    defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront: window];

This displays a blue window. I hope this is the optimal approach.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteve McLeodView Question on Stackoverflow
Solution 1 - Objective CJason CocoView Answer on Stackoverflow
Solution 2 - Objective CjohndpopeView Answer on Stackoverflow
Solution 3 - Objective CRich CatalanoView Answer on Stackoverflow
Solution 4 - Objective CJeff YounkerView Answer on Stackoverflow
Solution 5 - Objective CSteve McLeodView Answer on Stackoverflow