Question

In my OS X app, using Interface Builder, I have a window that looks like this:

enter image description here

I'd like to add a button to the right-hand side, to achieve this:

enter image description here

If this is possible, how can I do it?

Was it helpful?

Solution

It is not possible to do with Interface Builder, however you can get it done with little bit of coding :

NSButton *closeButton = [window standardWindowButton:NSWindowCloseButton]; // Get the existing close button of the window. Check documentation for the other window buttons.
NSView *titleBarView = closeButton.superview; // Get the view that encloses that standard window buttons.
NSButton *myButton = …; // Create custom button to be added to the title bar.
myButton.frame = …; // Set the appropriate frame for your button. Use titleBarView.bounds to determine the bounding rect of the view that encloses the standard window buttons.
[titleBarView addSubview:myButton]; // Add the custom button to the title bar.

OTHER TIPS

Swift 2.2 and Auto Layout, Create an "OK" button to the right of the title bar:

let myButton = NSButton()
myButton.title = "OK"
myButton.bezelStyle = .RoundedBezelStyle

let titleBarView = window!.standardWindowButton(.CloseButton)!.superview!
titleBarView.addSubview(myButton)
myButton.translatesAutoresizingMaskIntoConstraints = false
titleBarView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[myButton]-2-|", options: [], metrics: nil, views: ["myButton": myButton]))
titleBarView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-1-[myButton]-3-|", options: [], metrics: nil, views: ["myButton": myButton]))

With auto layout, you don't need to hard-code button's frame. And it is always on the right of the title bar even you resize the window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top