实施以下遇到问题:NSView Contraints - 如何强制子视图的纵横比保持不变?
+-------------------+
| |
| |
| RESIZABLE |
| |
| NSVIEW |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|+-----------------+|
|| ||
|| STATIC ||
|| ||
|| SUBVIEW ||
|| ||
|+-----------------+|
+-------------------+
当子视图的纵横比必须保持恒定(如用户改变可调整大小的视图的宽度)。
我想使用约束做,到目前为止,我已经有了:
//This is a snippet of the resizable view class
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
//This is a snippet of the subview class
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:aspectRatio constant:0]];
可调整大小的视图的框架初始设置。在这种情况下,不设置子视图的框架是有意义的?目前,主视图似乎消失了 - 它的宽度开始为零。
我应该如何解决这个问题 - 如果可能的话,我喜欢使用约束!
我会认为你展示的前两个约束是背对背的;你不想让子视图受到父视图(可调整大小)的限制吗? – trojanfoe 2013-05-13 11:54:54
@trojanfoe这就是我一开始想到的,但是这样做会产生一个错误:“无法在视图上安装约束,约束是否从视图的子树外引用某个东西?这是非法的。” – 2013-05-13 11:56:47
嗯,我不是专家,但我知道你可以表达约束WRT的超视图,但你可能需要使用该方法的视觉语言版本。 – trojanfoe 2013-05-13 11:58:11