NSPopover positions incorrectly when NSWindow frame is programmatically changed

392 Views Asked by At

I have a button in an NSWindow that brings up an NSPopover with:

popover = NSPopover()
popover?.behavior = .applicationDefined
popover?.contentViewController = self
let myDelegate = MyDelegate()
myDelegate.controller = self
popover?.delegate = myDelegate
popover?.show(relativeTo: hostingButton.bounds, of: hostingButton.superview!, preferredEdge: .minY)

This works great and I can drag the window around and resize it while the popover is open with zero problems.

The window, however, displays dynamic content and I have an option to keep it "compact" so to use the minimum height necessary for all of the current content to be shown.

When my code sets the NSWindow's frame with window.setFrame(newFrame, display: true), the popover moves to the other side of the screen. Its vertical alignment is perfect and matches the new window height, but its horizontal position is completely off.

The horizontal positioning on the Screen appears to be matching the x coordinate of the hostingButton within its superview, i.e. it sure looks like popover is using the x coordinate from the button's frame as a screen coordinate!?

Am I doing something wrong?

Is there a way of asking the popover, its hosting view, its superview, or its window to have another go at the positioning?

If not is there a way that I can manually move the popover window to the right place?

Any help would be much appreciated!

0

There are 0 best solutions below