Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIContextMenuInteraction : How to Dismiss Manually?

When a UIContextMenuInteraction/UITargetedPreview is visible, is it possible to manually dismiss it? For example, when the app goes to the background, I would like to dismiss it manually.

https://kylebashour.com/posts/context-menu-guide

like image 860
Gizmodo Avatar asked Oct 30 '25 23:10

Gizmodo


1 Answers

UIContextMenuInteraction has a method which is called dismissMenu.

I believe this is what you would rather call when in need to hide a menu, because calling dismiss(animated:completion:) could lead to unwanted behaviour if you have another modally presented controller and you are not sure if the menu is active.

Moreover, starting from iOS 13.2 you can get access to contextMenuInteraction as a property of a UICollectionView and call dismissMenu.

contextMenuInteraction is also available as a property for a UITableView starting from iOS 14.0

It becomes quite complicated when your minimal iOS version is 13.0 and you need somehow to dismiss a menu for a cell in a collectionView. This is a bump in the road I am currently having trouble with. I believe the only appropriate way is to create, save and provide a custom PreviewViewController in this method:

collectionView(collectionView: contextMenuConfigurationForItemAt: point:) -> UIContextMenuConfiguration?

When having access to your custom PreviewViewController, you can call dismiss(animated:completion:) method on it whenever you want.

like image 200
kridan Avatar answered Nov 02 '25 12:11

kridan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!