I want to be able to add a button to the title bar of all windows that open on a Mac.
The button will go on the right hand side, opposite the X - +
buttons.
This is asked about windows of my app here:
How can I create Yosemite-style unified toolbar in Interface Builder?
But I want the button to appear on all windows, of any app, that are opened on the Mac. Obviously, this will only happen once the user has installed this program.
I understand that this is essentially "plugging into" the OS's UI, but I have seen other apps do this, which makes me feel that it is do-able.
Here is screenshot where I want the button:
This is really a two-part question. As for how to get a button up there, I’d suggest using
-[NSWindow standardWindowButton:]
to get an existing window button and its superview (i. e. the title bar):The plugging-in is probably easiest to do as a SIMBL plug-in.
The officially-supported way to add a title bar button in OS X 10.10 (Yosemite) and later is by creating an
NSTitlebarAccessoryViewController
and adding it to your window using-[NSWindow addTitlebarAccessoryViewController]
.For example, I have a window that has a title bar accessory view:
To set this up, I started by adding a standalone view to the window controller scene in my storyboard. (You don't have to use a storyboard but I did in this project.)
My accessory view is an
NSView
with anNSButton
subview. The button title uses Font Awesome to display the pushpin.I connected the accessory view to an outlet (cleverly named
accessoryView
) in myNSWindowController
subclass:Then, in my window controller's
windowDidLoad
, I create theNSTitlebarAccessoryViewController
, set its properties, and add it to the window:This answer addresses the latest Xcode Version 9.3
Swift 4 - In
NSWindowController
add the below codeHope this is helpful.