启用NSScrollView滚动在Interface Builder中使用自动布局的内容 [英] Enabling NSScrollView to scroll its contents using Auto Layout in Interface Builder

查看:399
本文介绍了启用NSScrollView滚动在Interface Builder中使用自动布局的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了自定义的NSView 其中包含许多的NSTextField 和其他的NSView 秒。然后我嵌入在一个滚动视图自定义视图使用编辑>嵌入>滚动查看。这会在大纲适当的层次结构可见,但我需要再加入自动布局约束来指定这个滚动视图应放置在视图中(顶部,底部,领先,落后)。此外,我不得不添加自定义视图约束,坚决反对夹视图,从而奠定了在正确的位置的元素。这种运作良好,当我运行的所有元素恰当地出现在应用程序和观点并反弹滚动。然而,当我降低了主视图的高度,使不是所有的元素适合在屏幕上,自动布局出现警告,当我更新帧再次增加视图的高度。为了解决这个问题,我不得不删除滚动视图的底部约束到主视图。现在,当我运行应用程序时,窗口设置为合适的尺寸,但我不能滚动自定义视图来获取到内容的底部 - 这是受限制的,所以它不会,除了因为你的弹性反弹效果都滚动重新在边缘的限制。所以我的问题是,我必须为了让这个滚动视图,当我布置的所有元素在厦门国际银行,并使用自动布局滚动办?

I have implemented a custom NSView which contains many NSTextFields and other NSViews. I then embedded that custom view in a scroll view using Editor > Embed In > Scroll View. This creates the appropriate hierarchy as visible in the Outline, but I needed to then add Auto Layout constraints to specify where this scroll view should be placed within the view (top, bottom, leading, trailing). Additionally I had to add constraints for the custom view, set against the clip view, in order to lay out the elements in the correct location. This works well, when I run the app all the elements appear appropriately and the view does bounce scroll. However, when I reduce the height of the main view so that that not all of the elements fit on screen, auto layout warnings appear and when I update the frames it increases the height of the view again. To fix that, I had to remove the scroll view's bottom constraint to the main view. Now when I run the app, the window is set to the right size, but I cannot scroll the custom view to get to the bottom of the content - it's restricted so it won't scroll at all besides the elastic bounce effect because you're at the edge limit. So my question is, what must I do in order to allow this scroll view to scroll when I'm laying out all elements in a XIB and using Auto Layout?

推荐答案

这里的一般方法:


  • 请文档视图至少一样高夹子视图。或者,等价地,使不超过文档视图高夹视图。

  • 允许在文档视图的高度增长,但不会缩小超出它的子视图需要。

  • 在文档视图
  • prevent歧义由具有低优先级的约束,使之尽可能小给出的其他限制。

因此​​,例如,应该有剪辑视图的底部与文档视图的底部之间的约束,但它应该是一个不等式:Superview.Bottom&下; =文档View.Bottom。 (或者,等价地,文献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> =文本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 返回<$ C的子类$ C>是。然后,类笔尖夹视图设置为您的子类。在此之后,它会工作,只要你想。

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.

这篇关于启用NSScrollView滚动在Interface Builder中使用自动布局的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆