如何将自定义样式应用于 NSTableHeaderView?

How to apply custom styling to NSTableHeaderView?

所以我想要一个自定义外观的 NSTableView。我已经成功地子 classed NSTableRowViewNSTextFieldCell 来实现我想要的外观,但是我正在努力摆脱 [=31] 的默认样式=].我似乎可以调整它的框架,但是我不确定其余的默认样式来自哪里。

正如您在屏幕截图中看到的,红色区域是 headerView 的增加帧。我正在使用它的 CALayer 来设置颜色,但是我不知道如何更改里面的内容...

这是我在 ViewController

viewDidLoad 中所做的
override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    tableView.wantsLayer = true
    tableView.headerView?.frame = NSMakeRect(0, 0, (tableView.headerView?.frame.width)!, 32.00)
    tableView.headerView?.wantsLayer = true
    tableView.headerView?.layer?.backgroundColor = NSColor.red.cgColor
}

我也尝试过 subclassing NSTableHeaderView,但是这个 class 似乎在我可以进行的自定义方面非常有限...

任何帮助将不胜感激?

尝试使用此方法进入页眉。
如果答案对您有用,请记住将其排除在外。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //Color for the  header.
    let topColor = UIColor(red: (70/255.0), green: 000/255.0, blue: 000/255.0, alpha: 255)

    //Location of label.
    let locationOfLabel = self.view.frame.width

    let headerView:UIView = UIView()

    //Locating the text in the label
    let title = UILabel(frame: CGRect(x: 0, y: 30, width: locationOfLabel, height: 21))
    title.textAlignment = .center

    //Changing the title in the label per the default.
    let defaults:UserDefaults = UserDefaults.standard
    defaults.synchronize()

    let cardSelector  = defaults.object(forKey: "selectorKeyID") as! Int
    switch (cardSelector) {
    case 0: title.text = "Personal"
    break
    case 1: title.text = "Saved"
    break
    case 2: title.text = "Favorite"
    break
    case 3: title.text = "Grouped"
    break
    default:
        break
    }
    //Coloring the text in the label
    //Add the label
    title.textColor = UIColor.gray

    headerView.addSubview(title)

    //Adding a button to the header.
    let closeBttn = UIButton(type: UIButtonType.system) as UIButton
    closeBttn.frame = CGRect(x: 0, y: 30, width: 90, height: 27)
    closeBttn.setTitle("Close", for: UIControlState())
    closeBttn.setTitleColor(buttonColor, for: UIControlState())
    closeBttn.titleLabel?.font = UIFont.systemFont(ofSize: 19, weight: UIFontWeightMedium)
    closeBttn.addTarget(self, action: #selector(MainTableViewController.close), for: UIControlEvents.touchUpInside)
    headerView.addSubview(closeBttn)

    let menuButton = UIButton(type: UIButtonType.system) as UIButton
    menuButton.frame = CGRect(x: locationOfLabel-53, y: 30, width: 27, height: 27)
    menuButton.setBackgroundImage(UIImage(named: "VBC Menu4.png"), for: UIControlState())
    menuButton.addTarget(self, action: #selector(MainTableViewController.menuButton), for: UIControlEvents.touchUpInside)
    headerView.addSubview(menuButton)

    //Coloring the header
    headerView.backgroundColor = topColor

    //Rounding the corners.
    headerView.layer.cornerRadius = 10
    headerView.clipsToBounds = true


    return headerView

}

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 70.0
}

table 视图是基于视图的,但 header 不是,header 单元格仍然是 class NSTableHeaderCell。使用 NSTableColumn 的 属性 headerCell。您可以设置单元格的属性,例如 attributedStringValuebackgroundColor,或者用 NSTableHeaderCell 的子 class 实例替换单元格,并覆盖其中一种绘制方法。