79622067

Date: 2025-05-14 18:27:39
Score: 1
Natty:
Report link

To remove the padding, subclass NSTableCellView and inside overridden layout method, change the origin of the first subview like this:

import Cocoa

class NoPaddingCellView: NSTableCellView {
    override func layout() {
        super.layout()
        if let subview = subviews.first {
            subview.frame.origin.x = -6
        }
    }
}

class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

    let tableView = NSTableView()
    let items = ["Row 1"]

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("Column"))
        tableView.addTableColumn(column)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.focusRingType = .none
        
        tableView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(tableView)
        
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        ])
        
        selectFirstRow()
    }
    
    func selectFirstRow() {
        DispatchQueue.main.async {
            self.tableView.selectRowIndexes([0], byExtendingSelection: false)
        }
    }

    func numberOfRows(in tableView: NSTableView) -> Int {
        return items.count
    }

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let cell = NoPaddingCellView()
        let label = NSTextField(labelWithString: items[row])
        label.wantsLayer = true
        label.translatesAutoresizingMaskIntoConstraints = false
        cell.addSubview(label)

        NSLayoutConstraint.activate([
            label.leadingAnchor.constraint(equalTo: cell.leadingAnchor),
            label.trailingAnchor.constraint(equalTo: cell.trailingAnchor),
            label.topAnchor.constraint(equalTo: cell.topAnchor),
            label.bottomAnchor.constraint(equalTo: cell.bottomAnchor),
        ])

        return cell
    }
}

I described this fix in more details in this article:

https://x.com/TabFinderMac/status/1922705270230179960

Reasons:
  • Blacklisted phrase (1): this article
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Oleh Kopyl