10

We are converting our Swift based iOS app to Mac compatible using Catalyst in Xcode 11.

We are facing an issue in UI when user resize app window. So can we disable resize mode and give fix frame for app window?

Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81

3 Answers3

11

Beta 5 added a sizeRestrictions property to UIWindowScene.

If you set sizeRestrictions.maximumSize and sizeRestrictions.minimumSize to the same value, the window will not be resizable:

windowScene.sizeRestrictions?.minimumSize = CGSize(width: 640, height: 480)
windowScene.sizeRestrictions?.maximumSize = CGSize(width: 640, height: 480)

The easiest place to add this code is probably scene(_:willConnectTo:options:) in your scene delegate. The scene object passed in is a UIWindowScene, so just cast it and then set sizeRestrictions.

Note: sizeRestrictions are only available in iOS 10.15 Beta 5. This code will crash in older betas.

Adam
  • 4,405
  • 16
  • 23
  • where i need to write above code ? in SceneDelegate file ? – Hardik Thakkar Aug 08 '19 at 04:24
  • Thank adam for help, issue is crash issue is solve in macOS Catalina beta 5. – Hardik Thakkar Aug 08 '19 at 11:49
  • Yes, your scene delegate is probably easiest. I updated my answer. – Adam Aug 08 '19 at 17:33
  • I have same issue trying to disable resize mode for an ios calculator app and trying to convert it to mac by using catalyst . And this solution not working for me . – Hussain1982 Nov 11 '19 at 10:38
  • In the Xcode 12.2 beta using SwiftUI universal I do not have a SceneDelegate file. Where and how can I insert the code in my @main app file? – multitudes Oct 08 '20 at 09:30
  • @multitudes Hmm, that’s tricky. You could try @marcselosalloum’s solution, if you are okay having the same settings for all your windows. Otherwise… Maybe add a UIApplicationDelegateAdaptor and then use your app delegate’s configurationForConnecting connectingSceneSession method to attach a scene delegate? Here’s somebody trying that to mixed results: https://developer.apple.com/forums/thread/656631 – Adam Oct 08 '20 at 16:19
1

You can call this in your application:didFinishLaunchingWithOptions method:

    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)
    }
marcelosalloum
  • 3,481
  • 4
  • 39
  • 63
  • to piggy back off this, I have a similar issue, where the user resizes a catalyst project window, I would like to put some code in to redraw some views when the window is resized. What is the method for this? As this is an iOS project I dont know if there is a function such as 'viewDidResize'? – samp17 Nov 05 '19 at 16:14
  • @samp17: What are you using to set up the view sizes? Constraints or frames? – marcelosalloum Nov 05 '19 at 17:42
  • Constraints in storyboards – samp17 Nov 05 '19 at 20:34
  • 1
    @samp17 Check out [viewWillTransitionToSize:withTransitionCoordinator:](https://developer.apple.com/documentation/uikit/uicontentcontainer/1621466-viewwilltransitiontosize) in your view controller. – Adam Dec 10 '19 at 23:54
0

for Objective-C try

#if TARGET_OS_MACCATALYST
    for (UIScene* scene in UIApplication.sharedApplication.connectedScenes) {
        if ([scene isKindOfClass:[UIWindowScene class]]) {
            UIWindowScene* windowScene = (UIWindowScene*) scene;
            windowScene.sizeRestrictions.minimumSize = CGSizeMake(480, 640);
        }
    }
#endif