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.
Question posted in Xcode
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
2
Answers
I was interested in the same thing, and calling
UIViewController.dismiss(animated:completion:)
on the source view controller dismisses it as it would a modally presented controller.UIContextMenuInteraction
has a method which is calleddismissMenu
.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 aUICollectionView
and calldismissMenu
.contextMenuInteraction
is also available as a property for aUITableView
starting from iOS 14.0It 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:
When having access to your custom PreviewViewController, you can call
dismiss(animated:completion:)
method on it whenever you want.