Skip to content

Instantly share code, notes, and snippets.

@imxieyi
Created April 25, 2020 20:08
Show Gist options
  • Save imxieyi/a8f4ea34ceeb37faee4fae3a6612af31 to your computer and use it in GitHub Desktop.
Save imxieyi/a8f4ea34ceeb37faee4fae3a6612af31 to your computer and use it in GitHub Desktop.
Invoke iOS Context Menu Programmatically
class ContextMenuView: UIView {
var imageView: UIView!
var interaction: UIContextMenuInteraction!
func initSubviews() -> ContextMenuView {
interaction = UIContextMenuInteraction(delegate: self)
addInteraction(interaction)
let gesture = UITapGestureRecognizer(target: self, action: #selector(action(_:)))
addGestureRecognizer(gesture)
return self
}
@objc func action(_ sender: Any) {
// Invoke private API
interaction.perform(Selector("_presentMenuAtLocation:"), with: CGPoint.zero)
}
}
extension ContextMenuView: UIContextMenuInteractionDelegate {
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment