Search code examples
iosswiftkeyboarduipageviewcontrollerdismiss

How to dismiss keyboard from UIPageViewController's child ViewController


I have a View Controller that has a container that is UIPageViewController and has some child UIViewContorllers.

In the parent view Controller I have a searchBar and I want from child view Controllers to dismiss the keyboard of the parent view controller when tap on table view.

The problem is that if I add a tap gesture recognizer to the container it doesn't detect the tableview did select row delegate function that I need in some of the UIPageViewController child controllers. I imagine some ways to do it, with notification, delegates etc.. But is there any way to just dismiss keyboard from anywhere?


Solution

  • Yes, you can call ( if the embed field is in a subview ):

    self.view.endEditing(force: true)

    or ( it it's not )

    UIApplication.shared.windows.first?.endEditing(true)