Component

Context Menus

Learn to add Context Menus to your iOS App (Supports Xcode 16 and iOS 18)

Context Menus

Just copy the below code and modify according to your needs.

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()
    }
}

Time to build your iOS App?

ShipiOS provides you SwiftUI boilerplate, How-to Guides, Components Library and extra rewards!

Get ShipiOS Package
ShipiOS.App

I told you it's more than just a boilerplate :)

Made with ❤️ on 🌍

Need help? Reach out on:

© Copyright 2024 ShipiOS.App - All rights reserved.