NSPopover color

前端 未结 3 1793
终归单人心
终归单人心 2021-02-15 10:48

Is there any way to color NSPopover? Ive seen apps like facetab etc that have cool colors and resizeable popovers, how is this done?

Ay guides, hints?

Thanks.

相关标签:
3条回答
  • 2021-02-15 11:13

    Set popover.contentViewController.view as a subclass of NSView with a custom background drawing (i.e. override drawRect: and fill a rect with your custom background color).

    Then set the popover.appearance = NSPopoverAppearanceHUD to remove the default border around the view.

    Note that there will still be a very thin border around the view, so if you want to remove it completely, you may want to use MAAttachedWindow or a similar solution.

    0 讨论(0)
  • 2021-02-15 11:21

    You can use MAAttachedWindow instead.

    0 讨论(0)
  • 2021-02-15 11:28

    In Swift 4:

    1. Go to File->New File->Cocoa Class
    2. Name your class. eg. PopColor. Make sure it is a subclass of NSView
    3. Set the contents of the file to:
    import Cocoa
    
    class PopoverContentView:NSView {
        var backgroundView:PopoverBackgroundView?
        override func viewDidMoveToWindow() {
            super.viewDidMoveToWindow()
            if let frameView = self.window?.contentView?.superview {
                if backgroundView == nil {
                    backgroundView = PopoverBackgroundView(frame: frameView.bounds)
                    backgroundView!.autoresizingMask = NSView.AutoresizingMask([.width, .height]);
                    frameView.addSubview(backgroundView!, positioned: NSWindow.OrderingMode.below, relativeTo: frameView)
                }
            }
        }
    }
    
    class PopoverBackgroundView:NSView {
        override func draw(_ dirtyRect: NSRect) {
            NSColor.green.set()
            self.bounds.fill()
        }
    }
    
    1. In your storyboard, select the view which has your popover content and go to the Identity Inspector

    2. Set the Class to PopoverContentView

    Your popover and its triangle will now be green.

    0 讨论(0)
提交回复
热议问题