Preventing Popover Dismissal When Tapping Outside (Swift)
Asked Answered
G

5

10

I am trying to prevent a popup from being dismissed when the user taps outside of the popup. I have seem other questions/answers about this, and they all seem to suggest using the modalInPopover for the view. I have done this in the viewDidAppear as I have seen suggested. I have text fields along with buttons that fill in a label according to a selection from a dropdown menu. Before any information is entered, it works fine, and the popup is not dismissed when tapping outside. It also works fine for when text is entered in the text fields. However, as soon as I make a selection from a dropdown after tapping one of the buttons, the popup will dismiss after touching outside of it.

Are there any other suggestions as to why this could be? Could it have something to do with calling resignFirstResponder on the text fields?

Gwin answered 30/8, 2016 at 19:44 Comment(0)
J
15

In swift 3, ios 10

After implementing UIPopoverPresentationControllerDelegate the following function seems to do the trick.

func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
    return false
}

I hope this helps if anyone is still looking for a solution.

Joby answered 14/8, 2017 at 11:42 Comment(0)
M
1

You can implement the UIPopoverControllerDelegate:

func popoverControllerShouldDismissPopover(popoverController: UIPopoverController) -> Bool {
    //return true when you need
    return false
}

This is deprecated in iOS 9.0 but if you have a project which supports iOS 8 you have to use it.

Let me know if it works for you

Month answered 30/8, 2016 at 19:50 Comment(1)
Thank you so much for the suggestion, but unfortunately, it didn't work for me. Would you happen to know a way that would be compatible with iOS 9?Gwin
C
1

When displayed, taps outside of the popover window cause the popover to be dismissed automatically. To allow the user to interact with the specified views and not dismiss the popover, you can assign one or more views to the passthroughViews property. Taps inside the popover window do not automatically cause the popover to be dismissed. Your view and view controller code must handle actions and events inside the popover explicitly and call the dismiss(animated:) method as needed.

docs

Clear answered 18/12, 2019 at 16:12 Comment(0)
D
0

Update: Use UIPopoverPresentationControllerDelegate

func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
        return false
    }
Dagny answered 11/7, 2019 at 5:14 Comment(0)
P
0

For SwiftUI iOS 15.0+, iPadOS 15.0+, Mac Catalyst 15.0+, macOS 12.0+, tvOS 15.0+, visionOS 1.0+, watchOS 8.0+ there is new API available interactiveDismissDisabled(_:) which by defaults takes true parameter.

Example usage:

struct ContentView: View {
    @State private var isPresented = false


    var body: some View {
        Button("Show popover) {
            isPresented = true
        }
        .popover(isPresented: $isPresented) {
            Text("Demo")
                .interactiveDismissDisabled()
        }
    }
}

The same applies for sheets and inspectors. Just be careful to apply .interactiveDismissDisabled() to a Popover view itself, but not after .popover view modifier. In order to dismiss Popover programmatically use @Environment(\.dismiss)

Philosophize answered 12/9, 2024 at 8:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.