Search code examples
macoscocoapopupindicatornstoolbar

NSWindow's title as indicator popup button


I'm trying to make my first Cocoa app (previously I was making iOS apps) and what I wish to do for my custom view is make it's title clickable with indicator (accessory) triangle facing down. Clicking the title would open a popup/menu with my items.

How is that doneable in Cocoa?


Solution

  • Rdelmar's answer is probably the easiest way to go, but may not do exactly what you might want to do (which is replace the actual title with a pop up item, instead of having a popup button under the title in the toolbar area). With respect to functionality your application will probably work just as well using the toolbar.

    If, however, you truly want to replace the actual title, the means of going about this would be to set the NSWindow title text to @"" to hide it, and redraw it by sticking in your own view.

    [[[theWindow contentView] superview] addSubview:theSubview];
    

    This basically tells the superview of the main content view to add another subview (direct "translation" from the code), and you'll have to tinker with the frame of this new subview to have it be positioned where the title should be positioned (as now it's free to be placed anywhere in the window frame, including on top of the title bar, as opposed to simply inside the content view).

    theSubview can be your popup button, or whatever you want, and you'll also probably have to custom draw the popup button to match the original drawing of the window title.