swift – NSPopover内部的NSTableview看起来与众不同

我创建了一个类ListView,这是一个非常简单的Tableview.

如果我用IB实例化它看起来很好.
如果我以编程方式实例化它,也是一样的.

如果我在NSPopover中以编程方式实例化它,我的表格的每一行都会得到浅灰色背景.

《swift – NSPopover内部的NSTableview看起来与众不同》

这是从哪里来的?

这里的代码:

class ViewController: NSViewController {
    let popover = NSPopover()

    @IBOutlet weak var label: NSTextField!
    @IBAction func bu1(_ sender: Any) { 
        popover.show(relativeTo: label.visibleRect, of: label, preferredEdge: NSRectEdge.maxY)
    }

    override func viewDidLoad() { 
        super.viewDidLoad()

        let scrollListView = NSScrollView()
        let listView = ListView(frame: NSRect(x:100, y: 100, width: 100, height: 100))
        scrollListView.documentView = listView

        let viewController = NSViewController()
        viewController.view = scrollListView

        popover.appearance = NSAppearance(named: NSAppearance.Name.vibrantLight)
        popover.animates = false
        popover.contentViewController = viewController
    }
}

class ListView: NSTableView, NSTableViewDataSource, NSTableViewDelegate { 
    private var list = ["Tom","Jack","Susi"]

    required init?(coder: NSCoder) { 
        super.init(coder: coder)
        setup()
    }

    override init(frame frameRect: NSRect) { 
        super.init(frame: frameRect)
        setup()
    }

    private func setup() { 
        selectionHighlightStyle = NSTableView.SelectionHighlightStyle.regular
        rowSizeStyle = NSTableView.RowSizeStyle.small
        intercellSpacing = NSMakeSize(10.0, 0.0)
        headerView = nil
        target = self

        backgroundColor = NSColor.white
        for column in tableColumns { 
            removeTableColumn(column)
        }
        let column1 = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "text"))
        column1.isEditable = false
        column1.width = 200
        addTableColumn(column1)

        delegate = self
        dataSource = self

        self.reloadData()
    }

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

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

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        var cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "MyView"), owner: self) as? NSTableCellView
        if cellView == nil { 
            cellView = NSTableCellView(frame: NSZeroRect)
            let textField = NSTextField(frame: NSZeroRect)
            textField.isBezeled = false
            textField.drawsBackground = false
            textField.isEditable = false
            textField.isSelectable = false
            cellView!.addSubview(textField)
            cellView!.textField = textField
            cellView!.identifier = NSUserInterfaceItemIdentifier(rawValue: "MyView")
        }
        cellView!.textField!.stringValue = list[row]

        return cellView
    }
}

最佳答案 我最近遇到了这个问题,找不到令人满意的解决方案.

我现在设法做的方法是将NSTableView包装在NSEffectView中并将状态设置为非活动状态,如下所示查看:https://christiantietze.de/posts/2017/06/nssplitviewcontroller-visual-effects/(我已在IB中完成此操作)

它对正常/浅绿色外观确实有效.遗憾的是,在黑暗的外观中,TableView部分标题由于某种原因是透明的.我很想知道它在系统范围内的暗模式下会如何看待Mojave.

点赞