Is there a way to force the NSPopover to start in the detached state? I only see isDetached
which is a read-only property for the state of the popover and an NSPopoverDelegate method detachableWindow(forPopover:)
which lets me override the window that gets created. I'd like to essentially click a button and have the NSPopover start in the state in this photo.
The style of this window is exactly what a product requirement is and I can't seem to find any NSWindow style settings that would make a window do something like this (nor an NSPanel)
This detached popover functionality seems special in that it:
If you don't mind calling private API, it's actually pretty simple:
let detach = NSSelectorFromString("detach")
if popover.responds(to: detach) {
popover.perform(detach)
}
No need to even add a delegate. I don't know when this private method was added but it's available at least since macOS 10.13. I suspect it's available since the introduction of NSPopover
, though.
Here is the trick. Use the required delegate method detachableWindowForPopover:
to do the work for you, like:
- (void) showPopoverDetached
{
NSWindow* detachedWindow = [self detachableWindowForPopover:nil];
[detachedWindow.windowController showWindow:nil];
}
Seems that the Apple engineers implemented detachableWindowForPopover:
on a pretty smart way, I guess it uses the content view controller class, and will always create a singleton like instance of the detached window. Once detachableWindowForPopover:
has called the presented window instance will be re-used no matter when and why it is called, called it directly (from a func like my sample above) or indirectly (eg when you drag out, detach, the popover from its original position)
This way they can prevent a popover being detached 'twice' and we can also implement the detached way programmatically, nice job from them!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.