使用自动布局自动增长NSScrollView的文档视图? [英] Automatically grow document view of NSScrollView using auto layout?

查看:94
本文介绍了使用自动布局自动增长NSScrollView的文档视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来使NSScrollView适应使用自动布局时更改其文档视图的大小?

Is there a simple way to get an NSScrollView to adapt to its document view changing size when using auto layout?

我试图在文档视图,剪辑视图和滚动视图上同时调用setNeedsUpdateConstraints:setNeedsLayout:,但没有任何结果.

I have tried to call both setNeedsUpdateConstraints: and setNeedsLayout: on the document view, the clip view and the scroll view, without any results.

fittingSize报告正确的大小.

问题在于,包含子视图的文档视图在子视图更改大小时不会重新调整大小,即使它们调用invalidateIntrinsicContentSize也是如此.因此,文档视图的内容随着它们的增长而被裁剪为文档视图的原始大小.该文档视图是在笔尖中创建的,并在awakeFromBib方法中设置为滚动视图的文档视图.

The problem is that the document view, which holds subviews, is not re-sized when the subviews change their size, even if they call invalidateIntrinsicContentSize. The contents of the document view are hence clipped to the original size of the document view as they grow. The document view is created in a nib and set as the scroll view's document view in an awakeFromBib method.

我希望当文档的fittingSize更改时,文档视图框架会自动调整,并且滚动条也相应地更新.

I was hoping that the document view frame would automatically be adjusted when its fittingSize changes, and the scrollbars updated accordingly.

NSPopover做类似的事情-前提是内容控制器视图的子视图具有正确设置的约束,并且各种内容包含值都足够高(比隐藏的弹出窗口的最高约束优先级高).

NSPopover does something similar - provided that the subviews of the content controller's view have the constraints set right and various content hugging values are high enough (higher than the hidden popover window's hight constraint priority, for one).

推荐答案

当然,问题是当添加文档视图时,Cocoa会在插入文档视图的视图中自动创建一些硬约束,即剪辑视图.

The problem of course is that when adding the document view, Cocoa will automatically create some hard constraints in the view that the document view is inserted into, i.e., the clip view.

所以我自己的问题的答案很简单,只需使用:

So the answer to my own question is simple, just use:

// Assume self.docView is an IBOutlet populated with
// an NSView subclass
self.docView.translatesAutoresizingMaskIntoConstraints = NO;

在将文档视图添加到滚动视图之前:

before you add the document view to the scroll view:

self.scrollView.documentView = self.docView;

现在,由于剪辑视图中文档视图的布局没有自动生成的约束,因此您需要显式添加它们.否则,文档视图的内容将以其固有大小在滚动视图的左上角呈现.

Now, since there are no auto-generated constraints on the layout of the document view in the clip view, you will need to add them explicitly. Otherwise, the doc view's contents will just be rendered at their intrinsic size in the upper left corner of the scroll view.

这篇关于使用自动布局自动增长NSScrollView的文档视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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