【问题标题】:In a Mac Catalyst app, how do I set the draggable zone to move the window around the screen?在 Mac Catalyst 应用程序中,如何设置可拖动区域以在屏幕上移动窗口?
【发布时间】:2020-09-03 14:05:19
【问题描述】:

我的应用中有一个自定义“标题”,带有隐藏的标题栏。

如何将标题(一个简单的 UIView)设置为句柄区域来移动窗口?

【问题讨论】:

    标签: swift mac-catalyst


    【解决方案1】:

    在我的例子中,我有一个自定义的 UINavigationController,我需要覆盖 touchesBegan,但在你的例子中采用以下代码应该很容易。

    我们得到NSWindow 对象,它在 Mac Catalyst 中不直接可用,然后在我们的导航栏收到触摸时执行拖动事件。您可以在任何其他视图上使用它,它不需要是 UINavigation 项目。

    public extension UIResponder {
        
        func performMacCatalystWindowDrag() {
            #if targetEnvironment(macCatalyst)
            guard let nsApp = ((NSClassFromString("NSApplication") as? NSObject.Type)?.value(forKey: "sharedApplication") as? NSObject),
                let currentEvent = nsApp.value(forKey: "currentEvent") as? NSObject,
                let nsWindow = currentEvent.value(forKey: "window") as? NSObject else { return }
            nsWindow.perform(NSSelectorFromString("performWindowDragWithEvent:"), with: currentEvent)
            #endif
        }
    }
    

    并在您的自定义导航栏中覆盖touchesBegan

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        performMacCatalystWindowDrag()
        super.touchesBegan(touches, with: event)
    }
    

    以上代码不属于我,点击here查看更多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2017-11-15
      • 2018-03-28
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      相关资源
      最近更新 更多