Search code examples
iosswiftpopover

Preventing Popover Dismissal When Tapping Outside (Swift)


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?


Solution

  • 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.