Search code examples
cocoainterface-buildernspanel

In Interface builder, how can I add a custom button to a window title bar?


In my OS X app, using Interface Builder, I have a window that looks like this:

enter image description here

I'd like to add a button to the right-hand side, to achieve this:

enter image description here

If this is possible, how can I do it?


Solution

  • It is not possible to do with Interface Builder, however you can get it done with little bit of coding :

    NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton]; // Get the existing close button of the window. Check documentation for the other window buttons.
    NSView *titleBarView = closeButton.superview; // Get the view that encloses that standard window buttons.
    NSButton *myButton = …; // Create custom button to be added to the title bar.
    myButton.frame = …; // Set the appropriate frame for your button. Use titleBarView.bounds to determine the bounding rect of the view that encloses the standard window buttons.
    [titleBarView addSubview:myButton]; // Add the custom button to the title bar.