Objective c Cocoa/OSX-NSWindow standardWindowButton在复制和添加后表现异常
在我的应用程序中,我更改标准窗口按钮关闭/缩小/展开的位置,如下所示:Objective c Cocoa/OSX-NSWindow standardWindowButton在复制和添加后表现异常,objective-c,macos,cocoa,nswindow,nsbutton,Objective C,Macos,Cocoa,Nswindow,Nsbutton,在我的应用程序中,我更改标准窗口按钮关闭/缩小/展开的位置,如下所示: //Create the buttons NSButton *minitButton = [NSWindow standardWindowButton:NSWindowMiniaturizeButton forStyleMask:window.styleMask]; NSButton *closeButton = [NSWindow standardWindowButton:NSWindowCloseButton
//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符号
谁能告诉我我做错了什么。谢谢。为每个按钮调用
[按钮突出显示:是]
。您不会再次添加它们。您正在将它们移动到contentView。按钮最初位于window.contentView.superview中
[window.contentView.superview addSubview:closeButton];
[window.contentView.superview addSubview:fullScreenButton];
[window.contentView.superview addSubview:minitButton];
在不需要跟踪区域的情况下,应该可以获得正确的行为。以下是这种悬停魔法的机制:在绘制自身之前,标准的圆形按钮(例如
nsWindowMinimizeButton
)调用其超级视图未记录的方法\u鼠标组:
。如果此方法返回YES
则圆圈按钮会在其内部绘制图标。就这些
如果您将这些按钮放在自己的视图中,您可以简单地实现此方法并根据需要控制鼠标悬停的外观。如果您只是移动或重新显示这些按钮,而它们仍然是NSThemeFrame
(或类似内容)的子视图,那么您必须为该类swizzle方法\u mouseInGroup:
,而且可能不值得,因为我们有非常简单的前一个方法
在我的例子中,我有一个自定义的NSView
,它包含了我的标准按钮子视图
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];
}
我完全知道这个问题很老,而且是正确的。它防止使用任何专用API,而不像。只是想为那些不喜欢子类NSView
的人共享一个方法,以便将这些按钮放在一个已有的视图中(例如self.window.contentView
)
由于我只是想通过setFrame:
重新定位NSWindowButton
,我发现一旦窗口调整大小,跟踪区域似乎会自动“修复”自己,而不使用任何私有API(至少在10.11中)
因此,您可以执行以下操作,对重新定位按钮的窗口应用“假调整大小”:
NSRect frame = [self.window frame];
frame.size = NSMakeSize(frame.size.width, frame.size.height+1.f);
[self.window setFrame:frame display:NO animate:NO];
frame.size = NSMakeSize(frame.size.width, frame.size.height-1.f);
[self.window setFrame:frame display:NO animate:YES];
(我在主窗口的NSWindowDelegate
windowDidBecomeMain:
。只要窗口已加载且可见,就应该可以工作。)遗憾的是[按钮高亮显示:是]
仅绘制窗口按钮的“按下”状态()。我还不知道如何绘制“悬停”状态。@LiFumin您可能需要在Swift中将_mouseInGroup方法标记为“@objc”。@objc func\u mouseInGroup(ujc:Any)->Bool
`