Home > Net >  How to use swipeActions with popover in swift ui?
How to use swipeActions with popover in swift ui?

Time:04-08

I have a button that shows after the user swipes left on an HStack. When the user clicks on the button, I want to show a popover. However it doesn't work, nothing happens when the button is clicked. Here is my code:

struct SwipeItem: View {

    @State private var showPopover = false

    var body: some View {
        HStack(spacing: 0) {
            Text("text")
            Spacer()
        }
        .swipeActions(allowsFullSwipe: false) {
            Button("Modify", action: {
                showPopover = true
            })
            .tint(.green)
            .popover(isPresented: $showPopover) {
                Text("popover")
            }
        }
    }
}

I noticed that the popover worked when I moved the Button out of swipeActions (i.e. directly inside HStack). So it seems that swipeActions doesn't work with popover. What's the problem here? How to make them work together?

CodePudding user response:

I think you can move .popover modifer to set on HStack

var body: some View {
    HStack(spacing: 0) {
        Text("text")
        Spacer()
    }
    .popover(isPresented: $showPopover) {
        Text("popover")
     }
    .swipeActions(allowsFullSwipe: false) {
        Button("Modify", action: {
            showPopover = true
        })
        .tint(.green)
    }
}
  • Related