NSWindow Snippets

by Matt Long

Here are some snippets to help with performing different tasks using NSWindow.

Window Characteristics

  • Create Borderless Transparent Window: Create a new class that inherits from NSWindow and override the method: – (id) initWithContentRect: (NSRect) contentRect styleMask: (unsigned int) aStyle backing: (NSBackingStoreType) bufferingType defer: (BOOL) flag
    - (id) initWithContentRect: (NSRect) contentRect
                     styleMask: (unsigned int) aStyle
                       backing: (NSBackingStoreType) bufferingType
                         defer: (BOOL) flag
    {
        if (![super initWithContentRect: contentRect 
                                 styleMask: NSBorderlessWindowMask 
                              backing: bufferingType 
                             defer: flag]) return nil;
        [self setBackgroundColor: [NSColor clearColor]];
        [self setOpaque:NO];
     
        return self;
    }
    

    Note: This technique allows you to give the user the perception that Core Animation layers are being drawn onto the desktop. It can also be used to create windows that appear to have non-rectangular shapes.