隐藏Cocoa标题栏中的按钮

时间:2009-08-24 13:07:01

标签: cocoa window titlebar

苹果人机界面指南说:

alt text http://developer.apple.com/documentation/userexperience/Conceptual/AppleHIGuidelines/art/wn_panelcontrols.jpg

如何在该图像中创建第一个标题栏(仅使用关闭按钮)。禁用IB中的“调整大小”和“最小化”仅使调整大小/最小化按钮被禁用。但我希望它们消失。我怎么能这样做?

提前致谢。

4 个答案:

答案 0 :(得分:38)

我相信这应该有效:

[[window standardWindowButton:NSWindowCloseButton] setHidden:YES];
[[window standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
[[window standardWindowButton:NSWindowZoomButton] setHidden:YES];

答案 1 :(得分:5)

对于Swift

window!.standardWindowButton(.miniaturizeButton)!.isHidden = true
window!.standardWindowButton(.zoomButton)!.isHidden = true
window!.standardWindowButton(.closeButton)!.isHidden = true

setHidden

答案 2 :(得分:1)

另一种方式是......

for (id subview in [self window].contentView.superview.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"NSTitlebarContainerView")]) {
        NSView *titlebarView = [subview subviews][0];
        for (id button in titlebarView.subviews) {
            if ([button isKindOfClass:[NSButton class]]) {
                [button setHidden:YES];
            }
        }
    }
}

答案 3 :(得分:1)

我也需要这个,但鼠标悬停可见 - 斯威夫特:

var trackingTag: NSTrackingRectTag?
override func mouseEntered(with theEvent: NSEvent) {
    if trackingTag == theEvent.trackingNumber {
        window!.standardWindowButton(.closeButton)!.alphaValue = 1.00
    }
}
override func mouseExited(with theEvent: NSEvent) {
    if trackingTag == theEvent.trackingNumber {
        window!.standardWindowButton(.closeButton)!.alphaValue = 0.01
    }
}
func updateTrackingAreas(_ establish : Bool) {
    if let tag = trackingTag {
        window!.standardWindowButton(.closeButton)!.removeTrackingRect(tag)
    }
    if establish, let closeButton = window!.standardWindowButton(.closeButton) {
        trackingTag = closeButton.addTrackingRect(closeButton.bounds, owner: self, userData: nil, assumeInside: false)
    }
}
override func windowDidLoad() {
    window!.ignoresMouseEvents = false
    updateTrackingAreas(true)
    window!.standardWindowButton(.closeButton)!.alphaValue = 0.01
}
func windowShouldClose(_ sender: Any) -> Bool {
    window!.ignoresMouseEvents = true
    updateTrackingAreas(false)
    return true
}

需要可见性,但只需稍微 - 0.01不透明度,以使跟踪区域有效。