SwiftUI_visionOS_Note

.contextMenu()

Jan 11, 2024
1 min read|
  • Less popular than alert and sheet.
  • Another way of presenting bunch of buttons.
  • context menu is not used that much, because it’s hidden.
  • It’s only visible when user long press the screen.

1. Create a VStack

VStack(alignment: .leading, spacing: 10.0){
    Image(systemName: "house.fill")
                .font(.title)
            Text("0 to Vision Pro")
                .font(.headline)
            Text("How to use Context Menu")
                .font(.subheadline)
        }
        .padding(.all, 20.0)
        .glassBackgroundEffect()
swift

2. Add contextMenu()

VStack{...}
	.contextMenu(ContextMenu(menuItems:{
		Text("Menu Item 1")
		Text("Menu Item 2")
		Text("Menu Item 3")
		})
swift

3. Replace to button

Button(action: {print("HELLO")}, label: {
                Label("Button 1", systemImage: "flame.fill")
            })
swift
  • Context menu only accept Text and Image.

4. Increase # of Like

@State private var Counter: Int = 0

Button(action: {Counter += 1}
swift

  • Present a group of buttons is through a context menu.
  • It's not widely used as it's hidden and only visible through a long press.
  • Context menu also accepts a list of labels, each consisting of an icon and a title.

#swiftui #visionos #applevisionpro #visionpro

Subscribe to our newsletter

Get the latest news and updates from our team