NSPopover color

前端 未结 4 1658
遥遥无期
遥遥无期 2021-02-15 10:33

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.

4条回答
  •  广开言路
    2021-02-15 11:21

    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.

提交回复
热议问题