自定义OS X的window button

由于项目原来的设计,导致在OS X 10.10之后的系统上会出现一些奇怪的Bug,调用- addSubview:positioned:relativeTo:方法却并没有实现将视图放在最上层。经过不断的查找,才知道,子视图的父视图并没有被显示出来,导致了- addSubview:positioned:relativeTo:方法不起作用。那么如果将父视图显示出来的话,window button(关闭按钮,最小化按钮,全屏按钮,最大化窗口按钮)又会被遮盖掉,无奈,需要自定义window button。

在自定义之前一定要将系统自动创建的按钮关闭

1
2
3
4
[[self.window standardWindowButton:NSWindowCloseButton] setEnabled:NO];
[[self.window standardWindowButton:NSWindowMiniaturizeButton] setEnabled:NO];
[[self.window standardWindowButton:NSWindowFullScreenButton] setEnabled:NO];
[[self.window standardWindowButton:NSWindowZoomButton] setEnabled:NO];

自定义左上角的三个控制按钮

这里我是将三个按钮写到了一个自定义View中,方便布局用。

首先是头文件中声明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import <Cocoa/Cocoa.h>

@interface CustomWindowButtonView : NSView {
@private

BOOL mouseInside_;
NSButton *closeButton_;
NSButton *minitButton_;
NSButton *zoomButton_;
}

@property (nonatomic, assign) BOOL mouseInside;
@property (nonatomic, retain) NSButton *closeButton;
@property (nonatomic, retain) NSButton *minitButton;
@property (nonatomic, retain) NSButton *zoomButton;

@end

mouseInside参数是用于判断鼠标是否在视图之中。

创建一个window button所用到的方法是+ standardWindowButton:forStyleMask:
文档中相关说明:

Returns a new instance of a given standard window button, sized appropriately for a given window style.

这里说明了,此方法是返回一个标准的window button实例,并且会设置合适的window style。
NSWindowButton取值如下:

1
2
3
4
5
6
7
8
9
10
enum {
NSWindowCloseButton,
NSWindowMiniaturizeButton,
NSWindowZoomButton,
NSWindowToolbarButton,
NSWindowDocumentIconButton,
NSWindowDocumentVersionsButton = 6,
NSWindowFullScreenButton,
};
typedef NSUInteger NSWindowButton;

名字很清楚的显示了window button的作用,这里我们需要自定义的是左边的三个按钮,所以需要用到的是NSWindowCloseButton(关闭窗口按钮),NSWindowMiniaturizeButton(最小化窗口按钮)和NSWindowZoomButton(最大化窗口按钮,不是全屏按钮)。

注意
在OS X 10.10之后的版本中,NSWindowZoomButton会变成全屏按钮,而不是10.10之前的最大化窗口按钮。

第二个参数设置成self.window.styleMask就OK了。

这里windows button需要实现系统的悬停,响应窗口和非响应窗口的不同效果,那么就要实现鼠标移入和移出的方法- mouseEntered:- mouseExited:,但是实现这个方法,需要重写- updateTrackingAreas方法,因为当大小或坐标改变后就会造成所指定的检测区域错误,所以我们需要重写updateTrackingAreas方法,将创建NSTrackingArea的工作放在其中。

1
2
3
4
5
6
7
- (void)updateTrackingAreas {

[super updateTrackingAreas];

NSTrackingArea *const trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect) owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
}

然后继续实现鼠标移入移出的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)mouseEntered:(NSEvent *)event {
[super mouseEntered:event];

self.mouseInside = YES;
[self setNeedsDisplayForStandardWindowButtons];
}

- (void)mouseExited:(NSEvent *)event {
[super mouseExited:event];

self.mouseInside = NO;
[self setNeedsDisplayForStandardWindowButtons];
}

在这里我们需要实现一个在苹果的官方文档中并不存在的方法- _mouseInGroup:,这个方法用来返回判断鼠标是否在视图中的参数。

1
2
3
- (BOOL)_mouseInGroup:(NSButton *)button {
return self.mouseInside;
}

最后一个就是需要重绘一下按钮的状态:

1
2
3
4
5
- (void)setNeedsDisplayForStandardWindowButtons {
[self.closeButton setNeedsDisplay];
[self.minitButton setNeedsDisplay];
[self.zoomButton setNeedsDisplay];
}

这样我们就实现了自定义的window button,但是窗口在成为第一响应者和不适第一响应者的时候,还有当点击最小化的时候等等情况下,window button都会处于错误的状态中,例如,当失去第一响应的时候,这三个按钮应该变为淡灰色,但却是彩色,当点击最小化,然后再点击显示窗口的时候,三个按钮还是停留在鼠标移入的状态中,所以这些情况都要进行处理。这里我用通知实现了相关的处理:

首先,注册通知:

1
2
3
4
5
6
7
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(applicationWillBecomeActive:)
name:NSApplicationWillBecomeActiveNotification object:NSApp];
[defaultCenter addObserver:self selector:@selector(applicationDidResignActive:)
name:NSApplicationDidResignActiveNotification object:NSApp];
[defaultCenter addObserver:self selector:@selector(windowActiveChanged:)
name:NSWindowDidBecomeMainNotification object:nil];

然后实现处理方法(如果有更好的处理方式,大家可以自行写逻辑,这里仅供参考):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (void)applicationWillBecomeActive:(NSNotification *)notification {

self.mouseInside = YES;
[self setNeedsDisplayForStandardWindowButtons];

self.mouseInside = NO;
[self setNeedsDisplayForStandardWindowButtons];
}

- (void)applicationDidResignActive:(NSNotification *)notification {

self.mouseInside = NO;
[self setNeedsDisplayForStandardWindowButtons];
}

- (void)windowActiveChanged:(NSNotification *)notification {

self.mouseInside = NO;
[self setNeedsDisplayForStandardWindowButtons];
}

这样左边的三个自定义window button就已经完成。

兼容10.10之前的按钮需要实现自定义全屏按钮(NSWindowFullScreenButton)

创建全屏按钮的方式与之前相同,不多说直接看代码:

1
2
3
4
5
if (self.versionNum != NSNotFound) {
self.fullScreenButton = [NSWindow standardWindowButton:NSWindowFullScreenButton forStyleMask:self.window.styleMask];
[self.fullScreenButton setFrame:NSMakeRect(0, 0, self.fullScreenButton.frame.size.width, self.fullScreenButton.frame.size.height)];
[self.topView.fullScreenBackView addSubview:self.fullScreenButton];
}

在这里我判断了一下系统的版本。

注意

设置按钮一定要在主视图控制器中进行,不能在自定义的子视图中进行,否则在10.10之前的系统中会出现coreUI的错误提示。而且这个错误出现的几率不是100%

在全屏之后,我们最好将所有的window button给隐藏掉,调用setHidden:即可。

这下,自定义的window button就完成了。

参考资料