實施以下遇到問題: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