In my app I change the position of the standardWindowButtons close / miniturize / expand like so:
//Create the buttons
NSButton *minitButton = [NSWindow standardWindowButton:NSWindowMiniaturizeButton forStyleMask:window.styleMask];
NSButton *closeButton = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:window.styleMask];
NSButton *fullScreenButton = [NSWindow standardWindowButton:NSWindowZoomButton forStyleMask:window.styleMask];
//set their location
[closeButton setFrame:CGRectMake(7+70, window.frame.size.height - 22 - 52, closeButton.frame.size.width, closeButton.frame.size.height)];
[fullScreenButton setFrame:CGRectMake(47+70, window.frame.size.height - 22 -52, fullScreenButton.frame.size.width, fullScreenButton.frame.size.height)];
[minitButton setFrame:CGRectMake(27+70, window.frame.size.height - 22 - 52, minitButton.frame.size.width, minitButton.frame.size.height)];
//add them to the window
[window.contentView addSubview:closeButton];
[window.contentView addSubview:fullScreenButton];
[window.contentView addSubview:minitButton];
Now when the window appears with the buttons there is two problems: 1. They are grey and not their correct color 2. when the mouse is over them they do not show the + - or x sign
can anyone tell me what I am doing wrong. Thanks.
I'm fully aware that this question is old and Valentin Shergin's answer is correct. It prevents the utilize of any Private API, unlike Google did in Chrome. Just wanted to share a method for those who don't feel like subclass
NSView
just to put those buttons in an existed view (such asself.window.contentView
).As I just wanted to reposition the
NSWindowButton
s viasetFrame:
, I found out that once the window was resized, the tracking areas seems to "fix" themselves automagically, without any Private API usage (at least in 10.11).Thus, you can do things like the following to apply "fake resize" to the window that you repositioned your buttons:
(I did it within my main window's
NSWindowDelegate
windowDidBecomeMain:
. Should work as long as the window is loaded and visible.)Call
[button highlight:yes]
for each button.Here is the mechanics of this hover magic: Before drawing itself standard circled button (such as
NSWindowMiniaturizeButton
) calls theirsuperview
undocumented method_mouseInGroup:
. If this method returnsYES
circled button draws itself with icon inside. That's all.If you place these buttons inside your own view, you can simply implement this method and control this mouse-hover-appearance as you want. If you just move or relayout these buttons and they still be
subview
s ofNSThemeFrame
(or something similar), you have to swizzle method_mouseInGroup:
for this class, and probably it doesn't worth it because we have perfectly simple previous method.In my case I have custom
NSView
that contains my standard buttons assubview
s and this code makes all described above magic:You're not adding them again. You're moving them to contentView. The buttons are originally in window.contentView.superview.
Should get you the correct behaviour without requiring a trackingArea.