skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search