开发者

Order the color panel out

开发者 https://www.devze.com 2023-04-03 02:55 出处:网络
We all know this will bring the color panel forwards:开发者_StackOverflow中文版 [[NSApplication sharedApplication] orderFrontColorPanel:nil];, but how to hide it again? I tried orderOutColorPanel:, bu

We all know this will bring the color panel forwards:开发者_StackOverflow中文版 [[NSApplication sharedApplication] orderFrontColorPanel:nil];, but how to hide it again? I tried orderOutColorPanel:, but it does not work.


When in doubt, use brute force:

- (void)hideColorPanel {
    NSArray * wndws = [NSApp windows];
    for( NSWindow * w in wndws ){
        if( [w isKindOfClass:[NSColorPanel class]] ){
            [w orderOut:nil];
            break;
        }
    }
}


You are correct. There is no method on NSApplication to close the color panel. It is intended that the user will have control over the panel once it is shown.


As of in 2016. There is a method. in Swift 3, you can toggle NSColorPanel like this:

func toggleColorPanel(_ sender: NSButton) {
  if /*NSColorPanel.sharedColorPanelExists() &&*/ NSColorPanel.shared().isVisible {
        NSColorPanel.shared().orderOut(self)
    } else {
        NSApplication.shared().orderFrontColorPanel(self)
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消