MatterGoal
MatterGoal

Reputation: 16430

Borderless window and shadow in cocoa

I created a borderless window and i want to remove its shadow.

This is my init window code :

- (id)initWithContentRect:(NSRect)contentRect
                styleMask:(NSUInteger)windowStyle
                  backing:(NSBackingStoreType)bufferingType
                    defer:(BOOL)flag
{

    if (![super initWithContentRect: contentRect
                          styleMask: NSBorderlessWindowMask 
                            backing: NSBackingStoreBuffered
                              defer: NO]) return nil;

    [self setHasShadow:NO];
    [self setOpaque:NO];
    [self setBackgroundColor:[NSColor clearColor]];
    [self setAcceptsMouseMovedEvents:YES];
    return self;
}

As you can see i use setHasShadow:NO but nothing happen and shadow still shows.

How can i remove shadow ?

Upvotes: 2

Views: 2247

Answers (1)

Rob Keniger
Rob Keniger

Reputation: 46020

Well, for one thing you should be assigning the result of your call to super to self:

self = [super initWithContentRect: contentRect
                      styleMask: NSBorderlessWindowMask 
                        backing: NSBackingStoreBuffered
                          defer: NO];
if(self)
{
    //continue with initialisation
}
return self;

Also , if the window is being loaded from a nib it may have a shadow specified in Interface Builder. Since the settings in the nib are loaded after the init method is called, they may override the settings in your init method.

The solution then is to either make sure that the window does not have the Shadow appearance checkbox selected in Interface Builder, or call [self setHasShadow:NO] in ‑awakeFromNib rather than in the initialiser.

‑awakeFromNib is always called after the nib file has been loaded and all outlets are connected.

Upvotes: 3

Related Questions