I’ve created control which wraps button and popover. Here is the code:
struct PopoverButton<DestinationView: View>: View {
let label: LocalizedStringKey
let image: String
let destinationView: DestinationView
@State private var isVisible = false
init(label: LocalizedStringKey, image: String, @ViewBuilder _ destinationView: () -> DestinationView) {
self.label = label
self.image = image
self.destinationView = destinationView()
}
var body: some View {
Button(action: {
isVisible = true
}, label: {
Label(label, systemImage: image)
})
.popover(isPresented: $isVisible, content: {
destinationView
})
}
}
In general it works, but there is an issue when control is placed inside Menu
:
.toolbar {
ToolbarItem(placement: .automatic) {
PopoverButton(label: "working", image: "pencil") {
WorkingView()
}
}
ToolbarItem(placement: .automatic) {
Menu {
PopoverButton(label: "not.working", image: "arrow.down.right") {
NotWorkingView()
}
} label: {
Image(systemName: "plus")
}
}
}
In this case first PopoverButton
is working, second one placed in menu is displaying, but not showing popover. Flag is set to true, but popover callback is not fired.
Any suggestions?