在我的应用程序中,我更改了 standardWindowButtons 关闭/缩小/展开的位置,如下所示:
//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];
现在,当带有按钮的窗口出现时,存在两个问题:
1.它们是灰色的,而不是正确的颜色
2.当鼠标悬停在它们上方时,它们不显示 + - 或 x 符号
谁能告诉我我做错了什么。谢谢。
这是这种悬停魔法的机制: 在绘制自身之前标准圆形按钮(例如NSWindowMiniaturizeButton
)调用他们的superview
未记录的方法_mouseInGroup:
。如果这个方法返回YES
圆形按钮绘制自身并带有内部图标。就这样。
如果您将这些按钮放在您自己的视图中,您可以简单地实现此方法并根据需要控制此鼠标悬停外观。如果您只是移动或重新布局这些按钮,它们仍然是subview
s of NSThemeFrame
(或类似的东西),你必须调整方法_mouseInGroup:
对于这个类,可能不值得,因为我们之前的方法非常简单。
就我而言,我有自定义NSView
其中包含我的标准按钮subview
s 和这段代码使上述所有描述变得神奇:
- (void)updateTrackingAreas
{
NSTrackingArea *const trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect) owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
}
- (void)mouseEntered:(NSEvent *)event
{
[super mouseEntered:event];
self.mouseInside = YES;
[self setNeedsDisplayForStandardWindowButtons];
}
- (void)mouseExited:(NSEvent *)event
{
[super mouseExited:event];
self.mouseInside = NO;
[self setNeedsDisplayForStandardWindowButtons];
}
- (BOOL)_mouseInGroup:(NSButton *)button
{
return self.mouseInside;
}
- (void)setNeedsDisplayForStandardWindowButtons
{
[self.closeButtonView setNeedsDisplay];
[self.miniaturizeButtonView setNeedsDisplay];
[self.zoomButtonView setNeedsDisplay];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)