5

Is there any way to know when Menu control in SwiftUI is about to open OR open? I am sharing Menu code from implementation.

struct ContentView: View {
    var body: some View {
        Menu("Options") {
            Button("Order Now", action: placeOrder)
            Button("Adjust Order", action: adjustOrder)
            Button("Cancel", action: cancelOrder)
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
Santosh Singh
  • 765
  • 11
  • 27
  • I believe it's not possible to do that yet even with the latest swiftui, but you can achieve this with a custom menu. I also use custom menu/picker to achieve this. – Steven-Carrot Jul 13 '22 at 17:41
  • 1
    This might be helpful https://stackoverflow.com/a/72248463/12299030. – Asperi Jul 13 '22 at 17:42
  • I submitted feedback FB10667247 requesting sample code for this in the Menu doc page, it must be possible! I would guess we are just putting onAppear in the wrong place. – malhal Jul 13 '22 at 19:16
  • i don't think so. at least without new Xcode feature. – Steven-Carrot Jul 14 '22 at 17:51

1 Answers1

1

This is my try. Unfortunately the closure will fire once.

{
    var onFirstAppear: (() -> ())?

    var body: some View {
        Menu {
            Group {
                ForEach(data) { item in
                    Button(item.title) {
                        selection = item
                    }
                }
            }
            .onAppear(perform: onFirstAppear)
        } label: {
            label
        }
    }
}
SoftDesigner
  • 5,640
  • 3
  • 58
  • 47