NSStackView,隐藏其中一个子视图时填充视图

NSStackView, fill view when one of the subviews is hidden

我有一个具有此行为的垂直 NSStackView。它有两个子视图,其中一个位于堆栈视图的顶部,具有固定的高度。 然后,还有一个视图会覆盖视图中剩余的space。类似于下图。我当前的代码已经显示如下视图:

我想要一种行为,当我隐藏顶视图或 ViewA 时,ViewB 占据整个堆栈视图的高度。喜欢这张图片:

我正在以编程方式执行此操作,但是当我将 ViewA 设置为隐藏时,ViewB 不会占用整个 space 可用空间。将 ViewA space 留在那里。

我当前的代码已经显示了第一张图片中的 UI,它是:

@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

我不确定我需要添加什么才能在 ViewA 隐藏时增加 ViewB 的大小。 另外,我隐藏viewB后,调用了layoutSubtreeIfNeeded方法

最后问题出在三个不同的地方:

  1. 使用 -[NSStackView addSubView:] 代替 -[NSStackView addArrangedSubview:]
  2. 使用约束正确组织视图。
  3. 观点的拥抱优先级。

固定码为:

@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 addArrangedSubview:_viewA];
    [self addArrangedSubview:_viewB];

    _viewA.translatesAutoresizingMaskIntoConstraints = NO;
    [_viewA setContentHuggingPriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationVertical];
    [_viewA.heightAnchor constraintEqualToConstant:topBarViewHeight].active = YES;
    

    _viewB.translatesAutoresizingMaskIntoConstraints = NO;
    [_viewB setContentHuggingPriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationVertical];

    return self;
}

@end