Swift and SwiftUI tutorials for Swift Developers

How to show a Context Menu in SwiftUI

Instead of using swipe actions, we can present them in a context menu. In iOS, users typically long-press a list row to open a context menu. As with swipe actions, SwiftUI makes it easy to create a context menu.

In iOS, a pop-up menu is typically activated by a long press. A context menu consists of a collection of buttons, each with its own action, text, and icon.

In the following example, we create a context menu with the contextMenu modifier:

  Text("Options")
            .contextMenu {
                Button {
                    print("Make Favorite")
                } label: {
                    Label("Make Favorite", systemImage: "heart")
                }
    
                Button {
                    print("Share")
                } label: {
                    Label("Share Location", systemImage: "square.and.arrow.up")
                }
            }

In Xcode the result would be the following:

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Article

How to add custom swipe action buttons to a List row in SwiftUI

Next Article

How to use UIKit in SwiftUI

Related Posts