使用NSLayoutConstraints调整当前视图的大小 [英] Resize current view using NSLayoutConstraints
问题描述
我正在尝试使用NSLayoutConstraint
s设置视图的布局.
I'm trying to set the layout of a view using NSLayoutConstraint
s.
我可以在视图中正确设置内容,但是如果约束需要,我还需要能够调整当前视图的大小(我正在为其设置约束的视图).换句话说,我想通过一个灵活的窗口来设置固定的内容大小,而不是相反.
I can set things up within the view correctly, but I also need to be able to resize the current view (the one that I'm setting the constraints for) if the constraints require it. In other words, I want to set a fixed content size with a flexible window, rather than the reverse.
所以基本上,我有:
NSDictionary *views = [NSDictionary dictionaryWithObjectsAndKeys:self.v1, @"v1",
self.v2, @"v2",
nil];
// Set horizontal constraints
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[v1]-|"
options:0
metrics:nil
views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[v2]-|"
options:0
metrics:nil
views:views]];
// Set vertical constraints
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[v1]-[v2]-|"
options:0
metrics:nil
views:views]];
// Initialise height constraint for v1
self.v1Height = [NSLayoutConstraint constraintWithItem:self.v1
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:50];
如果以后要更改self.v1Height
(通过删除,重新创建然后读取),我希望self
的框架扩展(或收缩)以适应更改后的高度.
If I later change self.v1Height
(by removing it, recreating it, and then readding it), I would like the frame of self
to expand (or contract) to accommodate the changed height.
我不确定它是否相关(因为我比OS X对iOS更熟悉),但这是NSPopover
的内容视图.
I'm not sure if it's relevant (as I'm more familiar with iOS than OS X), but this is the content view of a NSPopover
.
我需要添加哪些约束来实现这一目标?
What constraint do I need to add to achieve this?
推荐答案
事实证明,NSPopoverController
是规则的例外,并且translatesAutoresizingMaskIntoConstraints
必须为YES
才能使自动布局正常工作.
As it turns out, NSPopoverController
is an exception to the rule, and translatesAutoresizingMaskIntoConstraints
must be YES
for autolayout to work correctly.
这篇关于使用NSLayoutConstraints调整当前视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!