I'm migrating my iOS app to support MacCatalyst but I'd like to prevent the window from being resized by the user.
Do you have any tips for that?
I'm migrating my iOS app to support MacCatalyst but I'd like to prevent the window from being resized by the user.
Do you have any tips for that?
Since Xcode11 Beta 5, the UIWindowScene
class started supporting the property sizeRestrictions
.
If you set sizeRestrictions.maximumSize
and sizeRestrictions.minimumSize
to the same value, the window will not be resizable. To do so, just call this in your application:didFinishLaunchingWithOptions
method (if you're using UIKit):
UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.forEach { windowScene in
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 480, height: 640)
windowScene.sizeRestrictions?.maximumSize = CGSize(width: 480, height: 640)
windowScene.sizeRestrictions?.allowsFullScreen = false // optional
}
If you're using SwiftUI instead of UIKit, you should actually add it to scene(_:willConnectTo:options:)
in your scene delegate.
Note: You need to run this in OSX 10.15 Beta 5 or later, otherwise it will crash
In a SwiftUI App lifecycle this worked for me:
import SwiftUI
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var userSettings = UserSettings()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(userSettings)
.environmentObject(KeyboardManager())
.onOpenURL(perform: { url in
let verificationCode = url.lastPathComponent
log.info(" Verification Code: \(verificationCode)")
userSettings.verificationCode = verificationCode
})
.onReceive(NotificationCenter.default.publisher(for: UIScene.willConnectNotification)) { _ in
#if targetEnvironment(macCatalyst)
// prevent window in macOS from being resized down
UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.forEach { windowScene in
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 800, height: 1000)
}
#endif
}
}
}
}
In the file, SceneDelegate.swift, add this:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.forEach { windowScene in
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 1268, height: 880)
windowScene.sizeRestrictions?.maximumSize = windowScene.sizeRestrictions!.minimumSize
}
guard let _ = (scene as? UIWindowScene) else { return }
}
for Objective-C try
for (UIScene* scene in UIApplication.sharedApplication.connectedScenes) {
if ([scene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene* windowScene = (UIWindowScene*) scene;
windowScene.sizeRestrictions.minimumSize = CGSizeMake(480, 640);
}
}
Steve gave me the answer. Also add disable fullscreen
WindowGroup {
ContentView()
.onReceive(NotificationCenter.default.publisher(for: UIScene.willConnectNotification)) { _ in
#if targetEnvironment(macCatalyst)
// prevent window in macOS from being resized down
UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.forEach { windowScene in
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 800, height: 1000)
windowScene.sizeRestrictions?.maximumSize = CGSize(width: 800, height: 1000)
windowScene.sizeRestrictions?.allowsFullScreen = false
}
#endif
}
}