I have a vertical NSStackView that has this behavior. It has two subviews, one of them at top of the stack view, that will have a fixed height. Then, there is another view that will cover the remaining space in the view. Similar to the image below. My current code already displays the view as below:
I want to have a behavior where, when I hide the top view, or ViewA, the ViewB takes the height of the entire stack view. Like this image:
I'm doing this programmatically, but when I set the ViewA as hidden, the ViewB doesn't take the entire space available. Leaving the ViewA space there.
My current code already shows the UI like in the first image and it is:
@interface CutsomStackView : NSSTackView
@property (nonatomic) NSView *viewA;
@property (nonatomic) NSView *viewB;
- (id)initWithFrame:(NSRect)frame views:(NSArray<NSView *> *)views;
@end
@implementation CutsomStackView
- (id)initWithFrame:(NSRect)frame views:(NSArray<NSView *> *)views
{
if (!(self = [super initWithFrame:frame]))
return nil;
_viewA = views[0];
_viewB = views[1];
self.detachesHiddenViews = YES;
self.orientation = NSUserInterfaceLayoutOrientationVertical;
self.spacing = 0;
self.distribution = NSStackViewDistributionFill;
CGFloat viewAHeight = NSHeight(_viewA.frame);
[self addSubview:_viewA];
[self addSubview:_viewB];
_viewA.translatesAutoresizingMaskIntoConstraints = NO;
_viewB.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = @{
_viewA,
_viewB
};
NSDictionary *metrics = @{
@"viewAHeight": @(viewAHeight)
};
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:UNLOCALIZED_STRING("V:|[_viewA(viewAHeight)][_viewB]|") options:NSLayoutFormatAlignAllLeading | NSLayoutFormatAlignAllTrailing metrics:metrics views:views]];
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:UNLOCALIZED_STRING("H:|[_viewA]|") options:0 metrics:nil views:views]];
return self;
}
@end
I'm not sure what do I need to add to allow the ViewB to grow in size when the ViewA is hidden.
In addition, after I hide the viewB, I call the layoutSubtreeIfNeeded
method.
At the end the problem was in three different things:
-[NSStackView addSubView:]
instead of-[NSStackView addArrangedSubview:]
The fixed code is: