我可以在UiKitForMac iOS应用程序上禁用窗口大小调整功能吗

时间:2019-07-12 19:34:55

标签: ios macos resize macos-catalina uikitformac

我正在迁移iOS应用程序以支持UIKitForMac,但我想防止用户调整窗口大小。

您对此有什么建议吗?

3 个答案:

答案 0 :(得分:1)

从Xcode11 Beta 5开始,UIWindowScene类开始支持属性sizeRestrictions

如果将sizeRestrictions.maximumSizesizeRestrictions.minimumSize设置为相同的值,则该窗口将无法调整大小。为此,只需在您的application:didFinishLaunchingWithOptions方法中调用它即可:

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

注意:您需要在OSX 10.15 Beta 5或更高版本中运行此程序,否则它将崩溃

答案 1 :(得分:1)

在 SwiftUI 应用程序生命周期中,这对我有用:

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

答案 2 :(得分:0)

在SceneDelegate.swift文件中,添加以下内容:

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