SwiftUI - Add Menu to navigationBarTrailing's ToolbarItem

Viewed 2570

I have a NavigationVeiw that has a ToolbarItem on the trailing side of its navigation-bar.

.toolbar {
    ToolbarItem(placement: .navigationBarTrailing) {
        Button(action: {}, label: {
            Text("Button")
        })
    }
}

navigationBarItems(leading:trailing:)is deprecated. Therefore, I am using toolbar(content:) with navigationBarTrailing placement. - Apple


I have used contextMenu on the Button.

   .toolbar {
        ToolbarItem(placement: .navigationBarTrailing) {
            Button(action: {}, label: {
                Text("Button")
            })
            .contextMenu(menuItems: {
                Text("Menu Item 1")
                Text("Menu Item 2")
                Text("Menu Item 3")
            })
        }
    }

It's not sensitive. You have to press hard to present it. I need to present it with a single touch like iOS Photo App's Add button. It is presented with a single touch.

How to show Menu with a single touch (not press)?

1 Answers

.contextMenu is a 3D Touch (press and hold) just use a regular Menu

.toolbar {
     ToolbarItem(placement: .navigationBarTrailing) {
         Menu(content: {
             Text("Menu Item 1")
             Text("Menu Item 2")
              Text("Menu Item 3")
         }, label: {Text("button")})
      }
  }
Related