【问题标题】:addCursorRect fails on NSButton after NSPopover loses focusNSPopover 失去焦点后,NSButton 上的 addCursorRect 失败
【发布时间】:2020-07-21 06:04:40
【问题描述】:

我有一个包含两个按钮的 NSPopover。当我打开弹出框时,以下代码可以在将鼠标悬停在按钮上并单击按钮时将光标更改为指向手,控制台中会出现“按下按钮”,并且会根据需要显示 NSColorPanel。

class Button: NSButton {
    override func resetCursorRects() {
        super.resetCursorRects()
        addCursorRect(bounds, cursor: .pointingHand)
    }
}

@IBAction func buttonTapped(action:Any) {

    print("Button pressed")

    let cp = NSColorPanel.shared
    cp.setTarget(self)
    NSColorPanel.setPickerMode(.none)
    cp.setAction(#selector(colorDidChange))
    cp.isContinuous = false
    cp.level = NSWindow.Level.statusBar
    cp.makeKeyAndOrderFront(self)

}

但是,如果我单击屏幕上的任何其他位置,然后返回到 NSPopover,当鼠标悬停在按钮上时,指针不再出现,而 onClick 事件仍然被触发(如“按下按钮”所示登录控制台),NSColorPanel 没有打开。

关于为什么会出现这种情况的任何建议?

【问题讨论】:

标签: swift macos nsbutton nspopover nscolorpanel


【解决方案1】:

我遇到了这个问题,为我解决的是将以下代码添加到弹出视图控制器viewWillAppear 函数中。

NSApplication.shared.activate(ignoringOtherApps: true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多