Mac Catalyst popover set/limit 大小问题

Mac Catalyst popover set/limit size problem

是否可以在 Mac Catalyst 上 limit/fix (min/max) 弹出窗口大小?请参阅附件视频。

Example video

是的,但这有点麻烦。 Big Sur 将这些呈现的视图控制器加载为它们自己的 windows,因此我们可以获取 window 的 windowScene 并设置其 sizeRestrictions。执行此操作的最佳(?)位置是在 presented 视图控制器的 viewWillLayoutSubviews 方法中:

class MyPresentedViewController: UIViewController {
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        if #available(macCatalyst 14, *) {
            view.window?.windowScene?.sizeRestrictions?.minimumSize = CGSize(width: 500, height: 500)
            view.window?.windowScene?.sizeRestrictions?.maximumSize = CGSize(width: 800, height: 800)
        }
    }
}

如果您根本不想调整显示的视图大小,只需将 minimumSizemaximumSize 设置为相同的值即可。

我不喜欢这样使用 viewWillLayoutSubviews,但是 windowSceneviewDidLoadviewWillAppear 中仍然是 nil,而在viewDidAppear,设置 sizeRestrictions 将在屏幕上显示可见的调整大小。

好消息是这个问题可能会在 Big Sur 11.1 中得到修复。根据 beta release notes,macOS 11.1 将尊重 preferredContentSize 它们默认情况下不会调整大小:

When you present a view controller with page sheet or form sheet presentation style, the size of the view controller’s root view is, by default, determined by the value returned from the presented view controller’s preferredContentSize method and the view is not resizable. You can arrange for the presented view controller to be resizable by using Auto Layout to specify the maximum and minimum sizes of its root view. To enable this, set the canResizeToFitContent property of the application’s main window to YES. One way to do this is to override the willMove(toWindow:) or didMoveToWindow() methods of a view in the main view controller. (65254666)