I need constraint the window size for an iOS app running on Mac.
That's easy for a MacApp, using
self.window?.minSize.width = 450
self.window?.maxSize.width = 450
or use
func windowDidResize(_ notification: Notification) { }
but how to achieve it in UIKit ?
You can set a min and max size in the UISceneDelegate method scene(_:willConnectTo:options:) by accessing the sizeRestrictions property of the UIWindowScene.
Here's an example:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let winScene = scene as? UIWindowScene else { return }
if let sizes = winScene.sizeRestrictions {
sizes.minimumSize = .init(width: 640, height: 480) // No smaller than 640x480
sizes.maximumSize = .init(width: .max, height: .max) // As big as the user wants
}
}