簡體   English   中英

我可以在我的 Mac Catalyst iOS 應用程序上禁用 window 調整大小功能嗎

[英]Can I disable the window resizing feature on my Mac Catalyst iOS app

我正在遷移我的 iOS 應用程序以支持 MacCatalyst,但我想防止 window 被用戶調整大小。

你有什么建議嗎?

從 Xcode11 Beta 5 開始, UIWindowScene類開始支持屬性sizeRestrictions

如果將sizeRestrictions.maximumSizesizeRestrictions.minimumSize設置為相同的值,則窗口將無法調整大小。 為此,只需在您的application:didFinishLaunchingWithOptions方法中調用它(如果您使用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)
    }

如果您使用的是SwiftUI而不是 UIKit,您實際上應該將它添加到場景委托中的scene(_:willConnectTo:options:)中。

注意:您需要在 OSX 10.15 Beta 5 或更高版本中運行此程序,否則會崩潰

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

在文件 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 }
}

對於 Objective-C 嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM