I've made an attempt to draw custom NSButtons, but it seems I'm reinventing the wheel here. Is there a way to just replace the default images used for the close, minimize and zoom buttons?
Several apps already do it:
- OSX 10.8's Reminders app (they appear dark grey when the window is not key, vs most appear light grey)
- Tweetbot (All buttons look totally custom)
More info:
I can generate the system defaults as such standardWindowButton:NSWindowCloseButton
. But from there the setImage
setter doesn't change the appearance of the buttons.
Best Solution
Edit: Since I wrote this, INAppStore has implemented a pretty nice way to do this with
INWindowButton
. If you're looking for a drag and drop solution check there, but the code below will still help you implement your own.So I couldn't find a way to alter the
standardWindowButton
s. Here is a walkthrough of how I created my own buttons.Note: There are 4 states the buttons can be in
On to the walkthrough!
Step 1: Hide the pre-existing buttons
Step 2: Setup the view in Interface Builder
You'll notice on hover the buttons all change to their hover state, so we need a container view to pick up the hover.
NSButton
s, each 14px wide x 16px tall inside the container view.Setup the buttons
Image
property for each button to the window-active-normal image.Alternate
image property to the window-active-press image.Bordered
off.Type
toMomentary Change
.close
,minimize
orzoom
(Below you'll see how you can use this to make the NSButton subclass simpler)Step 3: Subclass the container view & buttons
Container:
Create a new file, subclass NSView. Here we are going to use Notification Center to tell the buttons when they should switch to their hover state.
HMTrafficLightButtonsContainer.m
Buttons:
Create a new file, this time subclass NSButton. This one's a bit more to explain so I'll just post all the code.
HMTrafficLightButton.m
In IB set the Custom Class of the container view and all 3 buttons to their respective classes that we just created.
Step 4: Set the button actions
These methods, called from the view controller, are the same as the
standardWindowButton
s'. Link them to the buttons in IB.Step 5: Add the view to the window
I have a separate xib and view controller setup specifically for the window controls. The view controller is called HMWindowControlsController
Hope this helps. This is a pretty lengthy post, if you think I've made a mistake or left something out please let me know.