Cocoa / OSX - NSWindow standardWindowButton一旦复制并再次添加就会出现奇怪的行为 [英] Cocoa/OSX - NSWindow standardWindowButton behaving strangely once copied and added again

查看:710
本文介绍了Cocoa / OSX - NSWindow standardWindowButton一旦复制并再次添加就会出现奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我改变了standardWindowButtons的位置close / miniturize / expand如下:

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];

现在当窗口出现时,按钮有两个问题:
1.他们是灰色不是他们正确的颜色
2.当鼠标在它们上面时,他们不显示+或x符号

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.

推荐答案

这里是这个悬停魔法的机制:在绘制自己的标准圆圈按钮(如 NSWindowMiniaturizeButton )调用 superview 未记录的方法 _mouseInGroup:。如果此方法返回 YES 圆圈按钮自己绘制图标里面。

Here is the mechanics of this hover magic: Before drawing itself standard circled button (such as NSWindowMiniaturizeButton) calls their superview undocumented method _mouseInGroup:. If this method returns YES circled button draws itself with icon inside. That's all.

如果您将这些按钮放在自己的视图中,您可以简单地实现此方法并根据需要控制此鼠标悬停外观。如果你只是移动或继承这些按钮,他们仍然 subview NSThemeFrame (或类似的东西),你有to swizzle方法 _mouseInGroup:对于这个类,可能不值得,因为我们有完全简单的前一个方法。

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 subviews of NSThemeFrame (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.

在我的case我有自定义 NSView 包含我的标准按钮 subview s和这段代码使所有描述上面的魔法:

In my case I have custom NSView that contains my standard buttons as subviews and this code makes all described above magic:

- (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];
}

这篇关于Cocoa / OSX - NSWindow standardWindowButton一旦复制并再次添加就会出现奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆