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];
The problem is that you don’t want to call
display, you want to call eithermakeKeyAndOrderFrontororderFrontdepending on whether or not you want the window to become the key window. You should also probably useNSBackingStoreBuffered.This code will create your borderless, blue window at the bottom left of the screen:
You can make the sender for
makeKeyAndOrderFrontororderFrontwhatever is appropriate for your situation.