UIMenu
vs UIContextMenuInteraction
vs UIPointerInteraction
I'm trying to set up UIContextMenuInteraction
in the same way as in Files or Pages app:
- (Long) tap anywhere in the blank space shows the black horizontal
UIMenu
- Secondary (Right/Control) click with a pointer anywhere in the blank space shows context menu
See demo on the attached GIF below.
I'm able to set up UIContextMenuInteraction
and in its UIContextMenuInteractionDelegate
return the UIContextMenuConfiguration
with items I want to show.
The same for the small black UIMenu
, I could use UILongPressGestureRecognizer
and show the menu using UIMenuController.shared.showMenu
.
However, I'm not able to prevent UIContextMenuInteraction
from triggering and showing the UITargetedPreview
when long-pressing on a view and there seems to be now way of recognizing different UITouchType
s with the info provided to UIContextMenuInteractionDelegate
.
I also could not find how to show the context menu programatically, without UIContextMenuInteraction
. Is there a way to do that?
Question
How is this implemented in Files.app?
There is no way to programmatically trigger a context menu, but with some simple bookkeeping you can prevent it from showing when not wanted (e.g when touches are active on your responder).
To hide the preview, just return nil from
previewProvider
in UIContextMenuConfiguration's initializer.Here is a complete implementation with a view controller's view as the target: