0
votes

I want to have a window with a hidden title, as seen in Safari or Xcode, but using a titlebar accessory view instead of a toolbar (I want more control over arrangement and content than a toolbar provides). Is this doable?

I haven't quite been able to make it work. If I set my window's titleVisibility to hidden, and my accessory view's layoutAttribute is bottom, then the title area is empty except for the standard close/minimize/zoom buttons, and my accessory view appears below that.

If I change the layoutAttribute to right, then my accessory view appears to the right of the standard buttons where I want it, but the bottom is cut off because the title bar isn't tall enough, and the view also doesn't resize horizontally with the window.

Is there a way to make this work? Or do I have to use a toolbar?

Update: I used Xcode's visual debugger to examine Xcode's own title bar, and found that it is using a toolbar. The debugger refuses to attach to Safari, so I'm left wondering how it does the new tab button. I imagine that button is a right-pinned accessory view, and the rest is a normal toolbar. Safari's toolbar is still customizable, so that seems most likely.

1

1 Answers

0
votes

I decided to go ahead and use a toolbar, and it's working pretty well.

  • I took my NSTitleBarAccessoryController subclass and made it inherit from NSViewController instead.
  • I created a non-customizable toolbar for my window, with a single "Image Toolbar Item" in both the allowed and default sets.
  • The toolbar item has a height of 32 and a max width of 10000 so it can stretch to fill the title bar.
  • In my toolbar delegate, in toolbarWillAddItem:, I instantiate the view controller from the nib and put its view into the item.
  • A text label that is effectively the new window title has its value bound to the window's title.
  • To match the spacing in Xcode's title bar, use a left and right margin of 1 pixel (zero will cut off the edges) and a top margin of 5.
  • window.titleVisibility = .hidden moves the toolbar into the title area.