在我的应用程序中,我更改了 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
,则带圆圈的按钮会在内部绘制自己的图标。就这样。
如果您将这些按钮放在您自己的 View 中,您可以简单地实现此方法并根据需要控制此鼠标悬停外观。如果您只是移动或重新布局这些按钮,并且它们仍然是 NSThemeFrame
的 subview
(或类似的东西),您必须调整方法 _mouseInGroup:
对于此类,可能不值得,因为我们之前的方法非常简单。
在我的例子中,我有自定义的 NSView
,它包含我的标准按钮作为 subview
,这段代码使上述所有内容变得神奇:
- (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];
}
关于objective-c - Cocoa/OSX - NSWindow standardWindowButton 在复制并再次添加后表现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7634788/