我可以在我的 Mac Catalyst iOS 应用程序上禁用 window 调整大小功能吗
Can I disable the window resizing feature on my Mac Catalyst iOS app
我正在迁移我的 iOS 应用程序以支持 MacCatalyst,但我想防止 window 被用户调整大小。
你有什么建议吗?
从 Xcode11 Beta 5 开始,UIWindowScene
class 开始支持 属性 sizeRestrictions
。
如果您将 sizeRestrictions.maximumSize
和 sizeRestrictions.minimumSize
设置为相同的值,则 window 将无法调整大小。为此,只需在您的 application:didFinishLaunchingWithOptions
方法中调用它(如果您使用的是 UIKit):
UIApplication.shared.connectedScenes.compactMap { [=10=] 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 或更高版本中 运行 这个,否则会崩溃
在文件 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 { [=10=] 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 }
}
在 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 { [=10=] as? UIWindowScene }.forEach { windowScene in
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 800, height: 1000)
}
#endif
}
}
}
}
我正在迁移我的 iOS 应用程序以支持 MacCatalyst,但我想防止 window 被用户调整大小。
你有什么建议吗?
从 Xcode11 Beta 5 开始,UIWindowScene
class 开始支持 属性 sizeRestrictions
。
如果您将 sizeRestrictions.maximumSize
和 sizeRestrictions.minimumSize
设置为相同的值,则 window 将无法调整大小。为此,只需在您的 application:didFinishLaunchingWithOptions
方法中调用它(如果您使用的是 UIKit):
UIApplication.shared.connectedScenes.compactMap { [=10=] 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 或更高版本中 运行 这个,否则会崩溃
在文件 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 { [=10=] 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 }
}
在 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 { [=10=] as? UIWindowScene }.forEach { windowScene in
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 800, height: 1000)
}
#endif
}
}
}
}