NSStackView 中的隐藏视图没有隐藏?

Hidden view in NSStackView not hiding?

我创建了一个垂直的 NSStackView,它包含两个 NSView 子类(它们只是绘制背景颜色的 NSView)。我将堆栈视图设置为分离隐藏视图。我已将其中一个视图设置为隐藏。

两个视图都没有隐藏在堆栈视图中。

为了确保我没有发疯,我还设置了两个彼此相邻的相同 NSView,隐藏了一个。果然,有人躲了。

堆栈视图的分布设置为按比例填充(这似乎无关紧要)。

在 IB 中,行为似乎是正确的;其中一个视图隐藏了。

我一定漏掉了一些非常明显的东西,对吧?

如果相关,NSView 子类: #import "ViewWithBackgroundColor.h"

@implementation ViewWithBackgroundColor

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    [self.backgroundColor set];
    [NSBezierPath fillRect:dirtyRect];
    if(self.bottomBorderColor != nil) {
        NSBezierPath *linePath = [[NSBezierPath alloc] init];
        [self.bottomBorderColor set];
        linePath.lineWidth = 2.0;
        [linePath moveToPoint:NSMakePoint(0, 0)];
        [linePath lineToPoint:NSMakePoint(dirtyRect.size.width, 0)];
        [linePath stroke];
    }

}

- (NSColor *) backgroundColor {
    if (_backgroundColor) {
        return _backgroundColor;
    } else {
        return [NSColor clearColor];
    }
}

@end

这看起来像是 IB 和堆栈视图的问题(如果您还没有提交错误报告,请提交错误报告)。

要解决此问题,您可以:

  • 不要在IB中隐藏按钮,设置为运行时隐藏。

  • 取消选中 IB 中的 'Detaches Hidden Views' 堆栈视图 属性(在您的屏幕截图中可见),并在运行时使用 -[NSStackView setDetachesHiddenViews:].
  • 进行设置