Pat_Morita
Pat_Morita

Reputation: 3535

NSTableView remove padding/Space between Table view border and cells

I am on macOS, objective-c, XCode 12 - not iOS.

I created an NSTableView in a NSScrollView programmatically:

NSView* view = self.view;

// Create tableView
NSTableView* tableView = [[NSTableView alloc] initWithFrame:NSZeroRect]; // Size is set by constraints
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.backgroundColor = [NSColor redColor];
tableView.headerView = nil;
tableView.delegate = self;
tableView.dataSource = self;
tableView.rowHeight = 21; // matches cell height
tableView.intercellSpacing = NSMakeSize(1, 1);
NSNib *nib = [[NSNib alloc] initWithNibNamed:@"SHDataTableCellView" bundle:[NSBundle mainBundle]];
[tableView registerNib:nib forIdentifier:@"SHDataTableCellView"];

// Setup scrollView
scrollView.backgroundColor = [NSColor greenColor];
scrollView.hasVerticalScroller = YES;
scrollView.hasHorizontalScroller = YES;
scrollView.autohidesScrollers = YES;
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.documentView = tableView;
scrollView.contentView.drawsBackground = NO;
scrollView.scrollerStyle = NSScrollerStyleOverlay;
scrollView.automaticallyAdjustsContentInsets = NO;
scrollView.contentInsets = NSEdgeInsetsMake(0,0,0,0);
self.scrollView = scrollView;

[view addSubview:scrollView];

// Add constraints
NSDictionary *viewBindings = NSDictionaryOfVariableBindings(view,scrollView);

[view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[scrollView]-(0)-|" options:0 metrics:nil views:viewBindings]];
[view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[scrollView]-(0)-|" options:0 metrics:nil views:viewBindings]];

As you can see below all works fine. I was wondering how i can remove the outer "thicker" border around the cells as outlined below. I didn't find any inset settings or similar.

enter image description here

This is what i want:

enter image description here

If you need any more code, let me know (although i think this is the relevant part)

UPDATE:

Here's the view related delegate method i use to create the cell

// ################################################
- (NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    // Cell view is from a nib file which just has a text field with leading/trailing constaint = 0, height constraint = 21
    // Nib registered in table setup
    SHDataTableCellView *result = [tableView makeViewWithIdentifier:@"SHDataTableCellView" owner:tableView];
    // Height constraint, width is dynamic
    [result addConstraint:[NSLayoutConstraint constraintWithItem:result
                                                 attribute:NSLayoutAttributeHeight
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:nil
                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                multiplier:1.0
                                                  constant:21]];
    result.dataValue.stringValue = [[[self.datasourceController arrangedObjects] objectAtIndex:row] objectAtIndex:[tableColumn.identifier integerValue]];
    return result;
}

For more completeness i added a blue border to the cells view

self.wantsLayer = YES;
self.layer.borderColor = [NSColor blueColor].CGColor;
self.layer.borderWidth = 2.0f;

This looks like that:

enter image description here

So i assume its not a cell view issue but located somewhere in the table view itself (table has red background color).

Upvotes: 0

Views: 1030

Answers (1)

Pat_Morita
Pat_Morita

Reputation: 3535

I stumbled upon the solution when i tested the app on macOS 10.15.

It looked OK in Catalina but not in BigSur so i looked closer and i found the solution in a table style first available in BigSur. Once added this code in the table setup, it looks good in both systems.

 if #available(macOS 11.0, *) {
     table.style = NSTableView.Style.plain
 }

Upvotes: 5

Related Questions