且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有自动布局的 NSScrollView 在第一次手动调整窗口大小之前不会调整大小

更新时间:2023-08-20 10:59:58

我已经想出了一个答案 - 我对它并不完全满意,但它似乎有效.

I've worked out an answer - I'm not entirely happy with it, but it seems to work.

有些 Cocoa 小部件不能很好地处理自动布局 - 特别是,我发现*** NSWindows 和 NSTabViewItems 存在问题;我猜其他小部件也可能受到影响.本质上,这些是具有必须设置的***视图"的容器"小部件.如果包含"小部件是 NSScrollView(它本身将包含其他小部件),容器"小部件很难为包含"滚动视图建立大小.

There are certain Cocoa widgets that don't deal well with autolayout - in particular, I've found problems with top level NSWindows and NSTabViewItems; I'm guessing other widgets might also be affected. Essentially, these are "container" widgets that have a top level "view" that must be set. If the "contained" widget is an NSScrollView (which itself will contain other widgets), the "container" widget has difficulty establishing a size for the "contained" scroll view.

解决方法是重新启用 translatesAutoresizingMaskIntoConstraints 用于将用作包含"小部件的视图.在提供的示例中,在第 10 行创建的对象 scroll_view 是包含"小部件;在第 15 行调用 setTranslatesAutoresizingMaskIntoConstraints 的布尔值应该是 True,而不是 False.

The fix is to re-enable translatesAutoresizingMaskIntoConstraints for the view that will be used as the "contained" widget. In the example provided, the object scroll_view created on line 10 is the "contained" widget; the boolean value of the call to setTranslatesAutoresizingMaskIntoConstraints on line 15 should be True, not False.

这些问题在更新的 OS/X 版本中得到了改善 - Mavericks 没有 NSWindow 的问题,但它仍然存在 NSTabViewItem 的问题.但是,在较新版本的 OS X 上打开 translatesAutoresizingMaskIntoConstraints 似乎并没有造成任何损害;所有你都失去了 100% 自动布局解决方案的理论纯度.

These problems get better with more recent versions of OS/X - Mavericks doesn't have a problem with NSWindow, but it still has a problem with NSTabViewItem. However, it doesn't seem to do any damage to turn on translatesAutoresizingMaskIntoConstraints on newer versions of OS X; all you're losing it the theoretical purity of a 100% autolayout solution.