NSTableHeaderView adds a line?
Asked Answered
W

2

6

For my first Mac app, I'm trying to make a simple window with just a table view. I enabled Headers but it adds an annoying line on top of my NSTableHeaderView:

enter image description here

I can't seem to find a property to remove it. I know it can be removed, because the Finder doesn't have it:

enter image description here

When I disable Headers, the border is not there. I should also note that the NSTableView is located inside an NSSplitView. Any ideas?

Whomsoever answered 25/11, 2011 at 1:15 Comment(1)
I'm not seeing this behaviour. Can you just move the table view up by one pixel? Is it possible it's not at the absolute top of the view?Squid
F
0

The problem exists because both the window frame and the table view's scroll view have a 1px border. Depending on your layout you can either set the borderStyle of the NSScrollView that encloses your NSTableView to NSNoBorder (note that this will remove the 1px border from all sides of the scroll view). Or you can move the scroll view up by 1px.

Flick answered 25/11, 2011 at 10:17 Comment(2)
I already had set NSNoBorder, but the moving up by 1px solved it (can't believe I didn't think of that). It seems the Finder also does it like that, since there is indeed a 1px difference between the two images. Thanks for the help.Whomsoever
If the table view is inside a split view it might not be possible to move it up, because it is automatically arranged by the split view. In that case, just put a custom view into the split view and then put the table view into the custom view.Hadlee
P
3

Another solution is to:

  1. Subclass NSTableHeaderView
  2. Override -drawRect: to draw a 1pt white line horizontally across the top to match the table header view's background color.
  3. Instantiate an instance of the custom header view using -initWithFrame: passing in the existing headerView's frame.
  4. Assign the custom header view to the table view's headerView property.

Implementation of -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));
}

Or in 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()
    }
}
Peabody answered 4/1, 2017 at 5:46 Comment(0)
F
0

The problem exists because both the window frame and the table view's scroll view have a 1px border. Depending on your layout you can either set the borderStyle of the NSScrollView that encloses your NSTableView to NSNoBorder (note that this will remove the 1px border from all sides of the scroll view). Or you can move the scroll view up by 1px.

Flick answered 25/11, 2011 at 10:17 Comment(2)
I already had set NSNoBorder, but the moving up by 1px solved it (can't believe I didn't think of that). It seems the Finder also does it like that, since there is indeed a 1px difference between the two images. Thanks for the help.Whomsoever
If the table view is inside a split view it might not be possible to move it up, because it is automatically arranged by the split view. In that case, just put a custom view into the split view and then put the table view into the custom view.Hadlee

© 2022 - 2024 — McMap. All rights reserved.