How to change NSPopover background color include triangle part?

后端 未结 8 540
无人及你
无人及你 2020-12-02 23:25

How can I change NSPopover background color include triangle part?

\"enter

8条回答
  •  有刺的猬
    2020-12-03 00:04

    Swift 3

    override func viewDidMoveToWindow() {
    
        guard let frameView = window?.contentView?.superview else {
            return
        }
    
        let backgroundView = NSView(frame: frameView.bounds)
        backgroundView.wantsLayer = true
        backgroundView.layer?.backgroundColor = .white // colour of your choice
        backgroundView.autoresizingMask = [.viewWidthSizable, .viewHeightSizable]
    
        frameView.addSubview(backgroundView, positioned: .below, relativeTo: frameView)
    
    }
    

    If you want to change only the background colour of the popover (including the triangle/arrow), I figured that you don't need to create a subclass of NSView. A layer-backed NSView with a background colour should suffice.

    Also, you don't need to call super.viewDidMoveToWindow() because its default implementation does nothing.

提交回复
热议问题