Mac Catalyst允许调整窗口大小,是否有办法为Mac Catalyst应用提供最小窗口大小?
Mac Catalyst允许调整窗口大小,是否有办法为Mac Catalyst应用提供最小窗口大小?
只需将以下一块代码添加到您的application:didFinishLaunchingWithOptions
方法(适用于 UIKit 项目)或scene(_:willConnectTo:options:)
(适用于SwiftUI 项目)中:
UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.forEach { windowScene in
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 480, height: 640)
}
备注:你也可以在那里设置maximumSize
属性。
备注2:如果你将minimumSize
和maximumSize
都设置为相同的值,窗口大小将保持不变且无法改变大小。
didFinishLaunchingWithOptions
中窗口场景还没有设置好,所以它不起作用;添加延迟可以使其正常工作。 - hyouuufunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
addWindowSizeHandlerForMacOS()
return true
}
func addWindowSizeHandlerForMacOS() {
if #available(iOS 13.0, *) {
UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.forEach { windowScene in
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 800, height: 1100)
windowScene.sizeRestrictions?.maximumSize = CGSize(width: 800, height: 1100)
}
}
}
在我的应用程序中,我定义了一个名为addWindowSizeHandlerForMacOS()的函数,从第23行开始。然后我在AppDelegate.swift的application()中调用它(第18行)。
在您的场景代理中,您可以执行以下操作:
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).
guard let scene = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst)
scene.sizeRestrictions?.minimumSize = CGSize(width: 800, height: 1100)
scene.sizeRestrictions?.maximumSize = CGSize(width: 1280, height: 1200)
#endif
}
使用此方法实现Objective-C类:
- (void)setMinimumSize:(CGSize)size {
id app = NSClassFromString(@"NSApplication");
id shared = [app valueForKeyPath:@"sharedApplication"];
id mainWindow = [shared valueForKeyPath:@"mainWindow"];
NSValue *nssize = [NSValue valueWithCGSize:size];
[mainWindow setValue:nssize forKeyPath:@"minSize"];
}
@marcelosalloum的回答的Objective-C版本:
for (UIScene* scene in UIApplication.sharedApplication.connectedScenes) {
if ([scene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene* windowScene = (UIWindowScene*) scene;
windowScene.sizeRestrictions.minimumSize = CGSizeMake(480, 640);
}
}
使用sizeRestrictions?.minimumSize
或sizeRestrictions?.maximumSize
来设置您的 Mac 应用程序窗口的最小或最大尺寸。
打开您的SceneDelegate
并执行以下操作:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
scene.sizeRestrictions?.minimumSize = CGSize(width: 800, height: 600)
scene.sizeRestrictions?.maximumSize = CGSize(width: 800, height: 600)
}
只需将以下内容添加到您的Scene Delegate中:
window?.windowScene?.sizeRestrictions?.minimumSize = CGSize(width: 900.0, height: 750.0)
我还添加了如何将最大大小设置为最大可能值的说明:
window?.windowScene?.sizeRestrictions?.maximumSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
我做了一些小研究。因此,我在我的SceneDelegate的func scene中添加了以下内容:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if UIDevice.current.systemName.contains("Mac") {
window?.windowScene?.sizeRestrictions?.minimumSize = CGSize(width: 1024, height: 768)
} else {
// iOS - nothing to change
}
我不确定在iOS中是否需要系统名称子句,因为window.windowScene.sizeRestrictoins为nil,但是为了清楚理解和可能的未来更新,我添加了它。
我选择1024x768作为最小的iPad横向尺寸(Catalyst将设备报告为带有Mac OS的iPad横向模式)。