Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add buttons to Mac window Title Bars, system-wide

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:

Screenshot

like image 613
Timsk Avatar asked Mar 31 '12 12:03

Timsk


2 Answers

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:

demo window title bar

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.)

accessory view

My accessory view is an NSView with an NSButton subview. The button title uses Font Awesome to display the pushpin.

I connected the accessory view to an outlet (cleverly named accessoryView) in my NSWindowController subclass:

outlet connection

Then, in my window controller's windowDidLoad, I create the NSTitlebarAccessoryViewController, set its properties, and add it to the window:

@IBOutlet var accessoryView: NSView!
var accessoryViewController: NSTitlebarAccessoryViewController?

override func windowDidLoad() {
    super.windowDidLoad()
    createAccessoryViewControllerIfNeeded()
}

fileprivate func createAccessoryViewControllerIfNeeded() {
    guard self.accessoryViewController == nil else { return }
    let accessoryViewController = NSTitlebarAccessoryViewController()
    self.accessoryViewController = accessoryViewController
    accessoryViewController.view = accessoryView
    accessoryViewController.layoutAttribute = .right
    self.window?.addTitlebarAccessoryViewController(accessoryViewController)
}
like image 84
rob mayoff Avatar answered Sep 24 '22 18:09

rob mayoff


This answer addresses the latest Xcode Version 9.3

  • Go to a storyboard.
  • Drag and drop a toolbar in the storyboard to a Window.

enter image description here

  • The toolbar will be visible below a title.

enter image description here

  • Locate the Window on the storyboard

enter image description here

  • Check "Hide Title" in properties of the Window.

enter image description here

  • The toolbar is a part of the title now.

enter image description here

like image 37
frido Avatar answered Sep 21 '22 18:09

frido