MacOS 开发收录

MacOS 开发(十):NSViewController + N

2020-04-02  本文已影响0人  SoaringHeart
3221585812942_.pic_hd.jpg

🌰🌰:

    lazy var popover: NSPopover = {
        let controller = ListViewController()
        controller.preferredContentSize = CGSize(width: kScreenWidth*0.15, height: kScreenHeight*0.25)
        let popView = NSPopover.create(controller: controller)
        return popView
    }()

    func showPopoverController(_ sender: NSView) {
        popover.show(sender, preferredEdge: .maxY)
    }
@objc public extension NSPopover {

    static func create(controller: NSViewController) -> Self {
        let popover: NSPopover = self.init()
        popover.appearance = NSAppearance(named: .aqua)
        popover.behavior = .transient
        popover.contentViewController = controller;
        /**
         applicationDefined : 默认值,不会自动关闭popover,ESC键也不能关闭,应用关闭时,popovoer会关闭
         semitransient: 点击popover以外的界面部分,不会自动关闭,但ESC按键可以关闭popover
         transient:  点击popvoer界面以外的部分,popover会自动关闭,ESC键可以关闭popover
         */
        popover.behavior = .transient
        
        // 4. 显示popover
        /**
         relativeTo : 类型NSRect 显示popover时,相对的那个区域,也就是popover的三角箭头会指向的矩形边界
         of:  NSView类型, 用来说明第一个参数(矩形范围)是属于那个控件的.
         preferredEdge : 枚举类型,用来说明矩形四条边的那一条边界
         */
//        popover.show(relativeTo: view.bounds, of: view, preferredEdge: .maxX)
        return popover as! Self;
    }
    
    func show(_ view: NSView, preferredEdge: NSRectEdge) {
        if isShown == true {
            close()
        }
        show(relativeTo: view.bounds, of: view, preferredEdge: preferredEdge)
    }
}
上一篇下一篇

猜你喜欢

热点阅读