Jiulong Zhao
Jiulong Zhao

Reputation: 1363

Order the color panel out

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

Upvotes: 1

Views: 425

Answers (3)

Kyle KIM
Kyle KIM

Reputation: 1431

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)
    }
}

Upvotes: 0

jscs
jscs

Reputation: 64002

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

Upvotes: 2

Mike Hay
Mike Hay

Reputation: 2856

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.

Upvotes: 0

Related Questions