Change selection color on view-based NSTableView

前端 未结 13 2082
别跟我提以往
别跟我提以往 2020-12-07 19:11

Standard highlighting color in OS X applications is blue.

Is it possible to change it to another color, e.g. gray?

Note that I am using the new view-based

13条回答
  •  萌比男神i
    2020-12-07 19:18

    Here is James Chen's solution in Swift 3. I've also added the delegate method.

    class MyNSTableRowView: NSTableRowView {
    
        override func drawSelection(in dirtyRect: NSRect) {
            if self.selectionHighlightStyle != .none {
                let selectionRect = NSInsetRect(self.bounds, 2.5, 2.5)
                NSColor(calibratedWhite: 0.65, alpha: 1).setStroke()
                NSColor(calibratedWhite: 0.82, alpha: 1).setFill()
                let selectionPath = NSBezierPath.init(roundedRect: selectionRect, xRadius: 6, yRadius: 6)
                selectionPath.fill()
                selectionPath.stroke()
            }
        }
    }
    

    NSTableViewDelegate:

    func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
        return MyNSTableRowView()
    }
    

提交回复
热议问题