我的应用程序中有一个自定义“标题”,带有一个隐藏的标题栏。
如何将标题(一个简单的 UIView)设置为移动窗口的 handle 区域?
最佳答案
在我的例子中,我有一个自定义的 UINavigationController
,我需要重写 touchesBegan
,但在你的例子中采用以下代码应该很容易。
我们获取 NSWindow
对象,它在 Mac Catalyst 中不直接可用,然后在我们的导航栏接收到触摸时执行拖动事件。您可以在任何其他 View 上使用它,它不需要是 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查看更多。
关于swift - 在 Mac Catalyst 应用程序中,如何设置可拖动区域以在屏幕上移动窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63725309/