Using Context Menu with Previews in SwiftUI

SwiftUI’s contextMenu modifier is a simple API that does a lot for us. I wrote about building context menus in both UIKit and SwiftUI recently. I did not go into detail about support for the custom previews that landed in iOS 16, iPadOS 16 and macOS 13. Let us fill that gap. Recap: Context menus in SwiftUI A context menu is equivalent of a right-click on touch devices. Using long‑press (or Control‑click on the Mac) on a view reveals a menu of options exactly where the user needs it. In pure SwiftUI we use .contextMenu to create these menus: ...

How to use Context Menus in UIKit

In this post we’re going to add context menus to a UIKit UITableView. The end goal is to have something that looks like this: Why Use Context Menus? Context menus are great for keeping things tidy while still offering useful features. They: Keep your UI minimal Only show options when needed Feel natural with the iOS long-press gesture Save you adding extra buttons all over the UI – the menu keeps them tucked out of sight until needed. What We’re Making We’ll show a short list of fruit. When we long-press any item we will show a context menu with three options: ...

How to use Context Menus in SwiftUI

The aim for this post to build a very simple List view. When you long-press a row you will see a classic iOS context menu. It will look a little like this: Why Context Menus? Context menus give users an easy way to discover secondary actions right where they’re looking. They: Keep the primary UI clean. Surface actions only when they’re relevant. Feel familiar thanks to the long-press gesture we already use across iOS. What We’ll Build We’ll create a simple list of fruit. A long-press on any row reveals three actions: ...