问题描述
在XCode 4.5中,Interface Builder尊重某些视图的internalContentSize,例如NSButton,但是我不能说服它在我自己的自定义子视图上尊重它.这将导致IB添加额外的约束,以强制执行IB中绘制的布局,从而导致在运行程序时不使用固有尺寸.
Interface Builder in XCode 4.5 respects the intrinsicContentSize for some views, e.g. NSButton, but I can't convince it to respect it on my own custom subviews. This causes IB to add extra constraints trying to force the layout drawn in IB, which then causes the intrinsic sizes to not be used when the program is run.
例如,考虑一个以窗口为中心的按钮,以及一个以窗口为中心的自定义视图…
For example, consider a button centered in a window, and a custom view centered in a window…
您可以看到,自定义视图受到四个约束,大概是因为IB不知道该视图的nativeContentSize.您可以更改添加了哪些额外约束,例如您可以强制将其设置为宽度和高度,但不能删除它们.
You can see that the custom view gets four constraints, presumably because IB doesn't know the view's intrinsicContentSize. You can change which extra constraints are added, e.g. you can force it to be width and height instead, but you can't delete them.
我现在正在通过搜索和删除awakeFromNib中的额外约束来解决此问题,但是必须有一种更好的方法来实现此目的.
I'm coping with this now by searching and deleting the extra constraints in my awakeFromNib, but there must be a better way to do this.
推荐答案
在Interface Builder中设置占位符固有内容的大小-如果可以的话,设置为猜测".
Set a placeholder intrinsic content size — a "guess," if you will — in Interface Builder.
- 选择您的自定义视图.
- 显示大小检查器( ).
- 将内在大小"下拉菜单从默认(系统定义)"更改为占位符".
- 根据视图的运行时宽度和高度输入合理的猜测.
这些约束在编译时已删除,这意味着它们将对正在运行的应用程序没有影响,并且布局引擎将在运行时适当地添加约束,以遵守视图的 intrinsicContentSize
.
These constraints are removed at compile-time, meaning they will have no effect on your running app, and the layout engine will add constraints as appropriate at runtime to respect your view's intrinsicContentSize
.
这篇关于如何使Interface Builder在基于约束的布局中尊重自定义视图的固有内容大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!