iOS 13 Context Menu Demo
- Nheng Vanchhay
- Aug 24, 2021
- 2 min read
Demo app for iOS context menus, including menu configuration and advanced preview options.
Introduction
In iOS 13, context menus replace 3D Touch peek & pop. Though I’m sad to see 3D Touch go, the UIContextMenu API is a fantastic replacement. It works on all devices (including iPad!) and has powerful features for building menus and customizing your previews. This guide starts with a basic menu, then progresses to some of the more advanced features, such as submenus and various custom previews. It’s a little long, so ⌘+F might be your friend. Now let’s buckle up and get started!.
Adding a menu to any old view
There are two important parts to setting up a context menu on a view. The first is the interaction, which is all wrapped up in the UIContextMenuInteraction class. However, creating an interaction requires a delegate, which brings us to the second part of creating a menu: the UIContextMenuInteractionDelegate protocol.
To get started, let’s set up a view controller with a blue square in its view. Our goal is allow the user to long-press or 3D Touch that view to open the menu, which looks like this:

Snip Code Controller


Adding a menu to UITableView
Adding a menu to a single view is great, but often we want to allow a user to perform actions on an entire list of objects. Luckily, UIKit does a lot of the heavy lifting for adding menus to table view rows - in fact, we don’t even need to create or add an interaction to the table view or cells. Instead, every method in UIContextMenuInteractionDelegate has a corresponding UITableViewDelegate method. By implementing the table view delegate method tableView(_:contextMenuConfigurationForRowAt:point:), we can create a menu that’s shown when a cell is pressed:

Snip Code Controller





Comments