When using Catalyst for the Mac, is there a function that is called when a user changes the window size?

Just curious if there is a function to be notified when a user changes the size of the window in using Xcode 11 and the checkbox to run an iPad app on the Mac. Thanks!

Replies

Did you check if


    func windowDidResize(_ notification: Notification) { }


is called ?

Thanks for answering! windowDidResize is part of the MacOS SDK, but when I read through the documentation and played around with it, I was able to call to a windowDidResize function by adding a NSNotificationCenter observer for "NSWindowDidResizeNotification"

Hi when I try to add the "NSWindowDidResizeNotification"

NotificationCenter.default.addObserver(self, selector: #selector(graph_reload), name: NSWindowDidResizeNotification, object: nil)


I got the following error:

'NSWindowDidResizeNotification' is unavailable in Mac Catalyst

can someone help me please ?

Put in in as a string ("NSWindowDidResizeNotification") instead when you add the observer, works for me. Not sure what Apple will think of that, but it is an AppKit app under the hood, sort of.

Not sure what Apple will think of that

I can’t speak for Apple as a whole, but using AppKit notifications via a literal string rather than the symbolic constant makes me kinda queasy. I figured there must be a better way, so I ran this past DTS’s Catalyst expert. He confirmed that there’s a way to be informed about resizes without resorting to such shenanigans:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

// MARK: - window size chance

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

super.viewWillTransition(to: size, with: coordinator)

print("new size: \(size)")

}

I reply late (and so we shall not see the post on the first page of the forum section).


Your question was about window, so I assumed (wrongly) it was Cocoa.


I mainly use viewWillTransition for rotation (didRotate obsoleted a long time ago), when I need to change constraints.

Otherwise, resize is normally taken care of by autoLayout.