I have a NSWindowController with an NSWindow,
I am having an issue where, if I set the controller's contentViewController, the frame of the NSWindow updates beyond my expectations
contentViewController The entire window remains the same size, but it just moves back to the original position
This is the code that updates the frame
self.contentViewController = loadingViewController
I confirmed this by printing out the window's frame in lldb, before and after this line:
▿ Optional<CGRect>
▿ some : (-1701.0, 439.0, 1042.0, 778.0)
▿ origin : (-1701.0, 439.0)
- x : -1701.0
- y : 439.0
▿ size : (1042.0, 778.0)
- width : 1042.0
- height : 778.0
(lldb) po self.window?.frame
▿ Optional<CGRect>
▿ some : (-1680.0, 416.0, 800.0, 778.0)
▿ origin : (-1680.0, 416.0)
- x : -1680.0
- y : 416.0
▿ size : (1042.0, 778.0)
- width : 1042.0
- height : 778.0
What is even more mysterious, is that I originally tried 3 separate places to catch the window moving to locate the line of code causing it
I registered as an observer:
NotificationCenter.default.addObserver(self, selector: #selector(windowMoved(notification:)), name: NSWindow.didMoveNotification, object: nil)
as well as setting symbolic breakpoints on:
-[NSWindow _windowMoved:] and -[NSWindow _setFrame:]
All 3 of these methods caught window movement when I manually dragged the window around.
However, none of the 3 caught the window moving when the frame was changed programmatically from that one line of code above ^^
Has anybody seen this before, or have an idea of how this could be?
Turned out to be too much effort to isolate this issue into a test project, so I decided to just put a patch in,
I just store the origin before setting the contentViewController, and reset it afterwards. This works for me, though I'm still unsure why this is happening
let originalFrameOrigin = self.window?.frame.origin ?? CGPoint(x: 100, y: 100)
self.contentViewController = loadingViewController
self.window?.setFrameOrigin(originalFrameOrigin)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With