Context menus in iOS13+ [CollectionView]

Doyeon
doyeona
Published in
2 min readJan 19, 2021

Use context menus to give people access to additional functionality related to onscreen items without cluttering the interface which is similar to Peek and Pop.

using context menus

📌 Peek and Pop: Accelerate actions in your app by providing shortcuts to preview content in detail view controllers.

Apple is replacing Peek and Pop with context menus in iOS13 and Why is it?

Apple introduced Peek and Pop in iOS 9 and Apple never managed to add 3D touch to the iPad and they have removed it from iPhone models.

Two key differences are:

  • Context menus are available on all devices running iOS 13 and later however Peek and Pop is only available on devices that support 3D Touch.
  • Context menus immediately display relevant commands however Peek and Pop requires a swipe up to view commands.

How to use it?

I added a context menu to the collectionView to edit and delete each cell. For you to do that, simply conform the protocol UICollectionViewDelegate. If not in collectionView, create UIContextMenuInteraction and add it to a view.

collectionView(_:contextMenuConfigurationForItemAt:point:)

Returns a context menu configuration for the item at a point.

So what i need to do is call another method configureContextMenu which returns UIContextMenuConfiguration and create menus to show. This is a very similar way to creating ActionSheet.

  • UIContextMenuConfiguration: An object containing the configuration details for the contextual menu.
  • Before displaying a contextual menu, the system asks your UIContextMenuInteraction Delegate to provide a UIContextMenuConfiguration object with details about the menus.

So i first created an object containing the configuration details of the menus in #2.

After that, add menus such as share, delete, favorite and many others. Next is to set the title and icons. If you have some other tasks when the menu is pressed, do it in here refer to line #4 to 9

When everything is set up, return UIMenu with the title of the menu and the menus that you just created such as edit, delete or add… in line # 11

Here’s the complete code:

But I think it’s very important to understand the Human Interface Guideline about Context Menu before trying it on your side. By just using context menu has great advantages and fast access to the content without opening it and digging into other options however, people don’t use it very often like peek and pop because they don’t know what are the features available for your app and it’s kinda hidden. Therefore it might be hard to configure the app features if you hide them in context menus.💦\

references:

--

--

Doyeon
doyeona

Passionate iOS developer creating extraordinary apps. Innovative, elegant, and collaborative. Constantly pushing boundaries.Let's build the future together📱✨🚀