objective-c - Cocoa/OSX - NSWindow standardWindowButton 在复制并再次添加后表现异常

标签 objective-c macos cocoa nswindow nsbutton

在我的应用程序中,我更改了 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 中,您可以简单地实现此方法并根据需要控制此鼠标悬停外观。如果您只是移动或重新布局这些按钮,并且它们仍然是 NSThemeFramesubview(或类似的东西),您必须调整方法 _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/

相关文章:

iphone - 如何从点数组中绘制填充形状并在该形状内获得点击?

iphone - 如何在 json 解析中从 iphone 中单击后退按钮的子类别中获取上一个类别?

iphone - 在 Objective-C 上使用嵌套 JSON 对象时遇到问题

cocoa - OS X 10.6 Snow Leopard 中的 NSTask 问题

macos - 如何发布 NSSystemDefined NSEvent

ios - 谷歌地图 - 本地图放大时让路线沿着街道

swift - 更改多个 CAShapeLayer 的绘制顺序

c++ - 将 "libusb.h"搜索路径添加到 Xcode 5 (OS X v10.9 (Mavericks))

objective-c - 在 Objective-C/Cocoa 中如何计算字符串的边界矩形?

swift - 从 CoreData 刷新 NSTreeController