且构网

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

使用 Interface Builder 中的 Auto Layout 启用 NSScrollView 滚动其内容

更新时间:2023-02-09 19:29:32

一般做法如下:

  • 使文档视图至少与剪辑视图一样高.或者,等效地,使剪辑视图不高于文档视图.
  • 允许文档视图增加高度,但不要缩小到超出其子视图所需的高度.
  • 通过设置低优先级约束来防止文档视图中的歧义,以便在考虑其他约束的情况下使其尽可能小.

因此,例如,剪辑视图的底部和文档视图的底部之间应该有一个约束,但它应该是一个不等式:Superview.Bottom <= Document View.Bottom.(或者,等效地,Document View.Bottom >= Superview.Bottom.)

So, for example, there should be a constraint between the clip view's bottom and the document view's bottom, but it should be an inequality: Superview.Bottom <= Document View.Bottom. (Or, equivalently, Document View.Bottom >= Superview.Bottom.)

在文档视图中,您可能在底部有一些文本字段或其他东西,并且它与文档视图的底部之间存在约束.使该约束成为不等式:Superview.Bottom >= Text Field.Bottom + 标准间距.

Within the document view, you probably have some text field or something at the bottom and a constraint between that and the document view's bottom. Make that constraint an inequality: Superview.Bottom >= Text Field.Bottom + standard spacing.

这将导致文档视图的高度不明确.它可以是任何大小,足以容纳其所有子视图.添加高度约束.将其优先级设置为 51,将其常量设置为 0.也就是说,它想让视图的高度为 0,但优先级非常低,因此几乎任何其他东西都会取代它.但它解决了歧义.

That will cause ambiguity as to the height of the document view. It could be any size tall enough to fit all of its subviews. Add a height constraint. Set its priority to 51 and its constant to 0. That is, it wants to make the view have 0 height, but at very low priority so almost anything else will supersede it. But it resolves the ambiguity.

如果你想允许水平滚动,你需要在水平方向上做同样的事情.

If you want to allow horizontal scrolling, you need to do the same general thing in the horizontal orientation.

更新:

还有另一种方法.配置文档视图中的约束,使其具有严格的大小(没有不等式).这通常是从其顶部到顶部子视图、从该子视图的底部到另一个子视图的顶部等以及从底部子视图的底部到文档视图的底部的一系列约束.导致尾随也是如此.

There's another approach. Configure the constraints within the document view to give it a strict size (no inequalities). That's usually a chain of constraints from its top to the top subview, from that subview's bottom to the top of another subview, etc., and from the bottom of the bottom subview to the document view's bottom. Same for leading to trailing.

那么,剪辑视图和文档视图之间唯一必要的约束是顶部和前导约束.

Then, the only necessary constraints between the clip view and the document view are top and leading constraints.

如果您在此配置中进行测试,您将能够调整大小并且滚动视图将滚动.所以,这很好.但是,当滚动视图的内容区域高于文档视图时,文档视图将固定在内容区域的底部.在这种情况下,您通常希望将其固定在顶部.

If you test in this configuration, you'll be able to resize and the scroll view will scroll. So, that's good. However, when the scroll view's content area is taller than the document view, the document view will be pinned to the bottom of the content area. You usually want it pinned to the top in that situation.

原因是剪辑视图没有翻转.此外,它正在调整其边界以匹配文档视图.因此,即使将文档视图固定在剪辑视图的顶部存在约束,剪辑视图的顶部也不是您期望的位置.剪辑视图将文档视图置于底部的 (0, 0).

The reason is that the clip view is not flipped. Also, it's adjusting its bounds to match the document view. So, even though there's a constraint to keep the document view pinned to the top of the clip view, the top of the clip view isn't where you expect it to be. The clip view puts the document view at (0, 0), which is at the bottom.

所以,最后一步是创建一个 NSClipView 的子类,它覆盖 -isFlipped 以返回 YES.然后,将 NIB 中的剪辑视图的类设置为您的子类.之后,它将按您的意愿工作.

So, the final piece is to create a subclass of NSClipView that overrides -isFlipped to return YES. Then, set the class of the clip view in the NIB to your subclass. After that, it will work as you want.