[英]Can I disable the window resizing feature on my Mac Catalyst iOS app
I'm migrating my iOS app to support MacCatalyst but I'd like to prevent the window from being resized by the user.我正在迁移我的 iOS 应用程序以支持 MacCatalyst,但我想防止 window 被用户调整大小。
Do you have any tips for that?你有什么建议吗?
Since Xcode11 Beta 5, the UIWindowScene
class started supporting the property sizeRestrictions
.从 Xcode11 Beta 5 开始,
UIWindowScene
类开始支持属性sizeRestrictions
。
If you set sizeRestrictions.maximumSize
and sizeRestrictions.minimumSize
to the same value, the window will not be resizable.如果将
sizeRestrictions.maximumSize
和sizeRestrictions.minimumSize
设置为相同的值,则窗口将无法调整大小。 To do so, just call this in your application:didFinishLaunchingWithOptions
method (if you're using UIKit ):为此,只需在您的
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)
}
If you're using SwiftUI instead of UIKit, you should actually add it to scene(_:willConnectTo:options:)
in your scene delegate.如果您使用的是SwiftUI而不是 UIKit,您实际上应该将它添加到场景委托中的
scene(_:willConnectTo:options:)
中。
Note: You need to run this in OSX 10.15 Beta 5 or later, otherwise it will crash注意:您需要在 OSX 10.15 Beta 5 或更高版本中运行此程序,否则会崩溃
In a SwiftUI App lifecycle this worked for me:在 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
}
}
}
}
In the file, SceneDelegate.swift, add this:在文件 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 }
}
for Objective-C try对于 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.