Painting Up There

I wondered how to paint "up there". It's easy once you know where to look :)

The Cocoa View hierarchy only talks about the hierarchy of controls in the content view, laid out in Interface Builder. That view hierarchy also extends up — the content view is hosted by a frame view which paints the window itself : background, title, shadow. That frame view is the parent of the window Close/Minimize/Maximize controls and of the toolbar show/hide control.

  • Painting in the title bar retrieve the frame view with [[window contentView] superview] and reparent your control with [frameView addSubview:myControl]. Why do that ? I never felt the help button in JSCocoa's console was well placed. Putting it up there seems to make more sense. As the toolbar is minimal and non customizable, the show/hide toolbar button won't be missed.
  • Painting in the toolbar retrieve the frame view and look for its child of class NSToolbarView. Reparent your control to it with addSubview. Why do that ? I wondered how iTunes paints its 'toolbar' controls. It looks like a toolbar on the right side, but the center HUD and left side play controls don't have labels. Why not use a toolbar to have the standard appearance and fill the missing parts with raw views ? Don't know if it's a good solution, but it works.

Sample Code Image:iconZip.png Painting Up

Image:Painting Up There - Cocoa View Hierarchy.png

With once again thanks to F-Script for all the spying.

Matt Gallagher
2008 11 27

Nice diagram! Very pretty.

I don't know if I would ever put a help button there in my own program but it's fun to see people try things just because it can be done.

Patrick Geiller
2008 11 27

Thanks !

I've always felt the title bar was underused. All that space for a tiny bit of text … that's a shame :)

Follow me on Twitter
Planet Cocoa

2011 02 22Distance field
2010 07 202Binding through NSApp
2010 05 122Forwarding invocations
2010 02 272Core Image black fringes
2010 02 21Quickest Way to Shell
2010 02 08Who's calling ?
2009 09 2138 ways to use Blocks in Snow Leopard
2009 08 182Bracket Mess
2009 08 124Taming JavascriptCore within and without WebView
2009 04 15Debugging with Activity Monitor
2009 03 25How Core Image Color Tracking works
2009 03 1510Custom NSThemeFrame
2009 03 10Which framework is running ?
2009 03 074CoreUI can paint pretty big
2009 02 18Localization with functions
2009 01 30Did you forget to nest alloc and init?
2009 01 16JSCocoa on the iPhone
2009 01 11Mixing WebView and JavascriptCore
2009 01 09Badge overflow
2009 01 09Find your Garbage Collection leaks with Instruments

Powered by MediaWiki