Chapters

Hide chapters

Catalyst by Tutorials

Third Edition · iOS 15 · Swift 5.6 · Xcode 13.3

Section I: Making a Great iPad App

Section 1: 7 chapters
Show chapters Hide chapters

5. Adding Some Context
Written by Andy Pereira

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you added multi-window support to your app using scenes.

In this chapter, you’ll learn about context menus, adding support for long-press menus on iPad and how these menus port to the Mac automatically.

By the end of this chapter, you’ll learn:

  • What contextual menus are and how they enhance your app’s experience.
  • How to add basic support for context interactions.
  • How to implement basic menu items.
  • How to make menu items dynamic.
  • How to implement hierarchical menus.

Ready to experience the exciting world of contextual menus? Great! It’s time to get started.

Introducing Context Menus

You might want to jump right in and start coding, but before you get started, you’ll need some context around the topic at hand (pun certainly intended). Before iOS 13, implementing long-press popovers and content previews was a messy affair, requiring you to hop across several different UIKit APIs.

Luckily, there’s a new kid in town for iOS 13: A unified content preview and context menu interaction called UIContextMenuInteraction.

By using this new mechanism and its associated helpers on UIView, you can easily add context menus that change their behavior according to the platform your app is running on. On iPad, you trigger context menus with a long-press gesture. On Mac, UIContextMenuInteraction brings up menus with a familiar gesture – right-clicking on an element.

Look at this feature in action in the Shortcuts app for iPad. This particular context menu incorporates both a content preview and a context menu.

Context menu example.
Context menu example.

Now that you’ve whetted your appetite for context menus, it’s time to jump right in and create your first interaction.

Adding a Context Interaction

The most sensible place to enable the Journalyst app’s context menus is in the sidebar. Why? Well, most actions you’d expect to perform via long-press or right-click will be taken on journal entries. Over the course of this chapter, you’ll add a context menu to the sidebar cell and progressively create a full set of handy journal entry actions.

Xibop goylowq reya.
Kinuz rutxivv nuca.

Opening a New Window

In the previous chapter, you learned about scenes and how they support multi-window configurations in your apps. You implemented a very nifty custom drag interaction that allows the user to drag a journal entry to create a new window.

Uxom ig For Muxneb redholq jabo.
Iyop aq Ban Dorzep hiqqong vole.

Creating a New Entry

Oftentimes, apps that manage lists of data include a menu action that lets its users create a new instance of a given entity. Given that it would be pretty reasonable for a user to expect a New Entry action in your app’s context menu, why not go ahead and add one?

Job Aqykm fodfubs yafe.
Mip Axmpp kuchelw ruda.

Adding an Image to an Entry

Another action that would be pretty useful to have in the context menu for journal entries is the ability to directly add images. Adding such an action is, again, similar to the actions you’ve already implemented.

Enr Equji legwiqb howu.
Ont Ejisa yuxjicv cinu.

Add an Entry to Favorites

The next menu action you’ll implement allows a user to add and remove a journal entry as a favorite. At present, there isn’t a way to filter by favorite entries. However, adding one is a worthwhile effort because it’s a good example of a menu item you’d see in apps. It also illustrates how to dynamically change the state of a menu item based on data.

Ufh de Miqebelay ej Quzogi mxer Jijaoqaloz hodxijt faqe.
Aly pe Jewakemoc ed Yuxele vzah Doguecowav kojdutx setu.

Sharing an Entry

Your journal entry context menu is really starting to take shape now, but you’re not done yet. In some situations, you want to expand a menu into another sub-menu or series of sub-menus. Doing this tidies up the root menu and groups actions that logically belong together.

Xsafi namkejc kodi.
Wsefu yensajm nita.

Deleting an Entry

There’s just one more action to go before you have a fully-armed and operational context menu. The last action you’re going to add exposes another path for the user to delete journal entries.

Coqagu mipbanm jiko.
Xesusi vixdamm pozo.

Trying it on macOS

Now that you’ve gone to the trouble of implementing context menus for iPad, you’ll find that you get the same support for free when running on Mac.

Lojmirj hetu gxewb em Bek.
Cazvubg jacu xjizd ek Wah.

Key Points

  • Context menus are a powerful way to expose alternate paths for common app actions.
  • iOS offers a unified mechanism for creating context menus that work on iPad and Mac.
  • Context menus can be as simple as singular actions or as complex as multi-level hierarchical menus.

Where to Go From Here?

In this chapter, you learned how to add support for contextual menus in your app. You enhanced the user’s experience by adding interaction using UIContextMenuInteraction. You added not just one or two, but six such menus.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now