Drew
Drew

Reputation: 1452

Minimize / miniaturize cocoa NSWindow without titlebar

I'm stuck! Obviously... because I'm posting a question here.

I have built my own custom window controls for my OS X / cocoa app. The close button works great -- no problems. The minimize button doesn't work at all when I disable the titlebar.

enter image description here

So when the title bar is on like the image above and I hit this method, the minimizing works fine:

ViewController.h

@interface ViewController : NSViewController {

    - (IBAction)minimize:(id)sender;        
    @property (strong) IBOutlet NSButton *btn_minimize;

}
@end

ViewController.m

@implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
    }

    - (IBAction)minimize:(id)sender {
      [[NSApp mainWindow] performMiniaturize:self];
    }

    -(IBAction)terminate:(id)sender {
        [NSApp terminate:self];
    }
@end

Then if I disable the title bar, that same method stops working. No errors, nothing. I've tried both: [[NSApp mainWindow] miniaturize:nil]; and also [[NSApp mainWindow] performMiniaturize:self];. Neither of which worked. Actually... the both work IF the title bar is on. But once I turn it off, neither work.

titlebar turned off

app with no title bar

Thoughts / comments?

Oh, I'm using Storyboards, Xcode 7, and am targeting 10.10 and using the 10.11 SDK if that matters at all.

Thanks in advance.

Upvotes: 5

Views: 3171

Answers (3)

user15478717
user15478717

Reputation:

Adding to Wez Furlong's answer, this is how to do it in Swift 5.

Assuming you have an NSWindow named "theWindow", do the following:

theWindow.styleMask = theWindow.styleMask.union(.miniaturizable)

Basically, you perform a set union of the window's existing style mask with the .miniaturizable option.

Upvotes: 0

Wez Furlong
Wez Furlong

Reputation: 4987

In my case, I wanted to completely remove the title bar and trigger the miniaturization through some other means (eg: a key assignment).

I found that ensuring that the window style mask includes NSMiniaturizableWindowMask was required in order for NSWindow::miniaturize to have an effect.

Upvotes: 0

Guilherme Rambo
Guilherme Rambo

Reputation: 2046

You have to keep the original "traffic light" buttons around and hide them manually.

Here's how I've configured the window:

self.titleVisibility = NSWindowTitleHidden;
self.titlebarAppearsTransparent = YES;
self.movableByWindowBackground = YES;

And here's how I've hidden the traffic lights:

for (id subview in self.contentView.superview.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"NSTitlebarContainerView")]) {
        NSView *titlebarView = [subview subviews][0];
        for (id button in titlebarView.subviews) {
            if ([button isKindOfClass:[NSButton class]]) {
                [button setHidden:YES];
            }
        }
    }
}

See sample project here.

Upvotes: 5

Related Questions