DEV Community

Cover image for Context Menus in iOS 18 - #30DaysOfSwift
Vaibhav Dwivedi
Vaibhav Dwivedi

Posted on

Context Menus in iOS 18 - #30DaysOfSwift

Day 14: Enhancing Interactions with Context ⚙️

Welcome back to the #30DaysOfSwift series!

Today, let's look at Context Menus. A powerful way to offer additional actions when users long-press or right-click on UI elements.

Image description

By integrating context menus into your SwiftUI app, you can create a smooth, intuitive experience for users when interacting with different elements.

What are Context Menus?

  • Context Menus provide additional actions that a user can perform on an element.
  • They are revealed via a long press or right-click, making it easy for users to access additional options without cluttering the UI.

Code Example: Context Menus in Action

struct ContextMenuExample: View {
    @State private var favorite: Bool = false

    var body: some View {
        VStack {
            HStack {
                Text("Long-press me!")
                    .font(.title2)
                    .padding()
                Spacer()
                if favorite {
                    Image(systemName: "heart.fill")
                        .foregroundColor(.red) // Customization: Heart icon if marked as favorite
                }
            }
            .frame(maxWidth: .infinity)
            .padding()
            .background(Color.blue.opacity(0.2)) // Custom background color
            .cornerRadius(10) // Rounded corners for aesthetic touch
            .contextMenu { // Adding the context menu
                Button(action: {
                    favorite.toggle()
                }) {
                    Label("Favorite", systemImage: favorite ? "heart.fill" : "heart")
                }

                Button(action: {
                    print("Shared!")
                }) {
                    Label("Share", systemImage: "square.and.arrow.up")
                }

                Button(action: {
                    print("Deleted!")
                }) {
                    Label("Delete", systemImage: "trash")
                        .foregroundColor(.red) // Custom color for danger actions
                }
            }
        }
        .padding()
    }
}
Enter fullscreen mode Exit fullscreen mode

Now it's your turn! Try adding context menus to elements in your app to make your interactions more intuitive and user-friendly!

The full series is available on my profile and the components can also be found at shipios.app/components.

Happy Coding! 🎨

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Top comments (0)

Image of Datadog

Keep your GPUs in check

This cheatsheet shows how to use Datadog’s NVIDIA DCGM and Triton integrations to track GPU health, resource usage, and model performance—helping you optimize AI workloads and avoid hardware bottlenecks.

Get the Cheatsheet

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay