Skip to content

Instantly share code, notes, and snippets.

@JunyuKuang
Last active November 2, 2023 23:32
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JunyuKuang/9295390c9b7b5bef89d9f956f524836a to your computer and use it in GitHub Desktop.
Save JunyuKuang/9295390c9b7b5bef89d9f956f524836a to your computer and use it in GitHub Desktop.
Customize/extend the window draggable area of your Mac Catalyst app.
//
// UIResponder+performWindowDrag.swift
// JonnyUtility
//
// Created by Jonny Kuang on 10/7/19.
// Copyright © 2019 Junyu Kuang <lightscreen.app@gmail.com>. All rights reserved.
//
import UIKit
public extension UIResponder {
/// Call this method inside a `UIResponder.touchesBegan(_:with:)` method to perform window drag.
///
/// This method has effect only when your app is running on macOS.
func performMacCatalystWindowDrag() {
// NSApplication.shared.currentEvent
// NSWindow.performDrag(with:)
#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
}
}
/* example
private class DraggableNavigationBar : UINavigationBar {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
performMacCatalystWindowDrag()
super.touchesBegan(touches, with: event)
}
}
*/
@liuliu
Copy link

liuliu commented Nov 2, 2023

Have you figured out how to disable the default drag behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment