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.
So basically, I have:
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];
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.
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?