Despite all the "Dismissing Keyboard" questions, there doesn't appear to be one referring to this specific scenario.
I am working on a ViewController with the following (simplified) hierarchy:
- UIView
- UIScrollView
- UIStackView
- PLCustomViewOne
- UIButton
- UITextField
- PLCustomViewTwo
- UIButton
- UITextField
- PLCustomViewOne
- UIStackView
- UIScrollView
The ViewController is loaded from an XIB as are the two PLCustomView's. I can't seem to get the view to endEditing
(dismiss the keyboard) when the user selects anything contained by the UIStackViews.
Adding a gesture recognizer to the view certainly will work; however, this requires that every view in the hierarchy gets the gesture recognizer added to it, which doesn't seem very scalable. There are actually quite a few subviews on the real version of this.
I have tried adding the dismissal in touchesBegan
to the UIView subclass of each of the custom views, but this only dismisses the keyboard if the user touches the view which is containing the firstResponder. For example, if the textField in PLCustomViewOne
is active then touching anywhere else in that view dismisses the keyboard, but touching outside it, say, PLCustomViewTwo
, does not.
scrollView.endEditing(true)
in the top-level UIView? The issue is that on that top-level UIView,touchesBegan
isn't being called. – Michael Voccola