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

How to use print statements in SwiftUI

Debugging SwiftUI views can feel a little challenging due to its declarative nature. In this post, we’ll explore how to add print statements into your SwiftUI code to observe state changes, view updates, and data flow. Why use print in SwiftUI? SwiftUI’s declarative paradigm means you describe what your UI should look like, and the framework handles when to update it. This can make it easier to reason about but harder to debug lifecycle events and UI-related state changes. print statements give you: ...