我正在尝试创建无边框窗口,并将窗口样式掩码设置为“NSBorderlessWindowMask”。 该窗口被子类化,并为 canBecomeKeyWindow 和 canBecomeMainWindow 返回“Yes”。 我必须在左上角的 View 中只获得最小化按钮。 我正在使用“[NSWindow standardWindowButton:NSWindowMiniaturizeButton forStyleMask:NSBorderlessWindowMask]”获取最小化按钮对象的实例 并将其添加为 subview 。 该按钮存在于 nsview 中。但呈灰色且已启用,并且也不呈黄色。 一旦我点击它,它就会变成黄色。鼠标悬停时,最小化按钮中出现的“-”不会出现。 我尝试了 [button setHighlighted:YES],它实际上显示为单击的。 我添加了跟踪区域来获取鼠标悬停。
_minButton = [NSWindow standardWindowButton:NSWindowMiniaturizeButton forStyleMask:NSBorderlessWindowMask];
[_minButton setFrameOrigin:NSMakePoint(_minButton.frame.origin.x + 7, _minButton.frame.origin.y)];
[_minButton setTarget:self.view.window];
[_minButton setAction:@selector(miniaturize:)];
[self.view addSubview:_minButton];
NSTrackingArea* trackingArea = [[NSTrackingArea alloc]
initWithRect:[_minButton bounds]
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways
owner:self userInfo:nil];
[_minButton addTrackingArea:trackingArea];
- (void)mouseEntered:(NSEvent *)theEvent{
[[_minButton cell]setHighlighted:YES];
}
- (void)mouseExited:(NSEvent *)theEvent{
[[_minButton cell]setHighlighted:NO];
}
最佳答案
尝试打电话
[_minButton setNeedsDisplay:YES];
在方法的末尾
关于objective-c - 以编程方式向 nsview 添加关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24299054/