6

对于我的第一个 Mac 应用程序,我正在尝试制作一个只有表格视图的简单窗口。我启用了标题,但它在我的顶部添加了一条烦人的行NSTableHeaderView

在此处输入图像描述

我似乎找不到可以删除它的属性。我知道它可以被删除,因为 Finder 没有它:

在此处输入图像描述

当我禁用标题时,边框不存在。我还应该注意,NSTableView位于NSSplitView. 有任何想法吗?

4

2 回答 2

3

另一种解决方案是:

  1. 子类NSTableHeaderView
  2. 覆盖-drawRect:以在顶部水平绘制一条 1pt 的白线,以匹配表头视图的背景颜色。
  3. -initWithFrame:使用传入现有headerView的框架实例化自定义标题视图的实例。
  4. 将自定义标题视图分配给表格视图的headerView属性。

实施-drawRect:

- (void)drawRect:(NSRect)dirtyRect {
    // Allow the table header view to draw itself
    [super drawRect:dirtyRect];
     // Draw a 1pt white line across the width of the header view
    [[NSColor whiteColor] setFill];
    NSRectFill(NSMakeRect(0.0f, 0.0f, self.bounds.size.width, 1.0));
}

或者在 Swift 中:

override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect)
    let topBorderBox = NSRect(x: 0, y: 0, width: bounds.size.width, height: 1)
    if dirtyRect.intersects(topBorderBox) {
        NSColor.white.setFill()
        topBorderBox.fill()
    }
}
于 2017-01-04T05:46:16.357 回答
0

问题的存在是因为窗口框架和表格视图的滚动视图都有 1px 的边框。根据您的布局,您可以将borderStyle包围NSScrollView您的设置NSTableViewNSNoBorder(请注意,这将从滚动视图的所有侧面移除 1px 边框)。或者您可以将滚动视图向上移动 1 像素。

于 2011-11-25T10:17:17.270 回答