I have an iOS app that I have enabled Catalyst for. One function in the app opens a new window. By default, this window opens very large but I need a way to make it smaller by default. I know you can set windowScene.sizeRestrictions?.minimumSize
and .maximumSize
, but that then limits the window to my preferred size. I'd like to make it so the window opens a certain size, say 500x800
by default, but can be expanded by the user to whatever they want.
I have tried window?.frame = CGRect(origin: .zero, size: CGSize(width: 500, height: 800))
in the SceneDelegate
, but it has no effect.
Visual example:
Figured it out, so I'll help anyone with this issue in the future. Set your .maximumSize
as your preferred size. Then after setting the window, use DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
to set the .maximumSize
again, but this time what you want to be the actual maximum window size.
My full code:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst)
let toolbarDelegate = NewSchoolworkToolbar()
let toolbar = NSToolbar(identifier: "main")
windowScene.title = "New Schoolwork"
if let titlebar = windowScene.titlebar {
titlebar.toolbar = toolbar
titlebar.toolbarStyle = .unified
titlebar.separatorStyle = .shadow
}
#endif
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let newClassworkView = NewClassworkTableViewController()
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 400, height: 500)
// This will be your "preferred size"
windowScene.sizeRestrictions?.maximumSize = CGSize(width: 500, height: 800)
window.rootViewController = newClassworkView
self.window = window
window.makeKeyAndVisible()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
// This will be your actual size.
windowScene.sizeRestrictions?.maximumSize = CGSize(width: 9000, height: 9000)
}
}