设置nspopover的位置

时间:2017-09-03 10:33:04

标签: swift macos nsviewcontroller nspopover

我有一个视图控制器(A),它将另一个视图控制器(B)显示为一个弹出窗口。

在我的VC(A)中是一个带有这个IBAction的NSButton:

self.presentViewController(vcPopover, asPopoverRelativeTo: myButton.bounds, of: myButton, preferredEdge: .maxX, behavior: .semitransient)

结果:

enter image description here

现在我想改变我的popover的位置 - 我想提升它。

我试过了:

let position = NSRect(origin: CGPoint(x: 100.0, y: 120.0), size: CGSize(width: 0.0, height: 0.0))
self.presentViewController(vcPopover, asPopoverRelativeTo: position, of: myButton, preferredEdge: .maxX, behavior: .semitransient)

但是这个位置没有改变

另一个例子

enter image description here

我有一个分段控件。如果你点击段" 1"将显示一个弹出框(与上面相同的代码)。但箭头指向段" 2"而是分段" 1"

1 个答案:

答案 0 :(得分:1)

首先,确保您的popover真的是NSPopover,而不仅仅是NSViewController。假设您要在popover中包含的视图控制器的故事板ID为" vcPopover",获取内容vc将如下所示:

let popoverContentController = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "vcPopover")) as! NSViewController

然后,将其包装在一个popover中:

let popover = NSPopover()
popover.contentSize = NSSize(width: 200, height: 200) // Or whatever size you want, perhaps based on the size of the content controller
popover.behavior = .semitransient
popover.animates = true
popover.contentViewController = popoverContentController    

然后,要提出,请致电show(relativeTo:of:preferredEdge:)

vcPopover.show(relativeTo: myButton.bounds, of: myButton, preferredEdge: .maxX)

这应该更新popover的位置。

更新:您可能正在使用NSSegmentedControl,这意味着您需要特别注意show传递的矩形。您需要在分段控件的坐标系中传递一个bound 来描述段的区域。这是一个详细的例子:

// The view controller doing the presenting
class ViewController: NSViewController {

    ...

    var presentedPopover: NSPopover?

    @IBAction func selectionChanged(_ sender: NSSegmentedControl) {
        let segment = sender.selectedSegment

        if let storyboard = storyboard {
            let contentVC = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("vcPopover")) as! NSViewController

            presentedPopover = NSPopover()
            presentedPopover?.contentSize = NSSize(width: 200, height: 200)
            presentedPopover?.behavior = .semitransient
            presentedPopover?.animates = true
            presentedPopover?.contentViewController = contentVC
        }

        presentedPopover?.show(relativeTo: sender.relativeBounds(forSegment: segment), of: sender, preferredEdge: .minY)
    }

}

extension NSSegmentedControl {

    func relativeBounds(forSegment index: Int) -> NSRect {
        // Assuming equal widths
        let segmentWidth = bounds.width / CGFloat(segmentCount)

        var rect = bounds
        rect.size.width = segmentWidth
        rect.origin.x = rect.origin.x + segmentWidth * CGFloat(index)

        return rect
    }

}

请注意,NSSegmentedControl的扩展名使用宽度计算段的近似矩形。此方法假定宽度相等,不考虑边框。您可以修改此方法以考虑您的需要。可以找到有关获取iOS段(类似)的帧的信息here

只要视图控制器存在于同一故事板中且故事板标识符为" vcPopover"

,此示例就会被验证为正常工作。