Window size of iOS app running on Mac

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 ?

Answered by RickMaddy in 882713022

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
    }
}
Accepted Answer

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
    }
}

Please note that the simple code I posted constrains the size on both the Mac and on the iPad. You may want different size restrictions for the Mac and iPad.

Thanks for the warning. I did limit use of the constraint when running on Mac.

Window size of iOS app running on Mac
 
 
Q