I have a search view controller with the search bar on top. When searching, the keyboard shows up and stay up even when accessibilityElementDidLoseFocus
is called on the search bar.
I would like to dismiss the keyboard when that happens. Can I do that without subclassing the search bar I am using?
Here is a way to do that without subclassing:
// Assuming you have an accessibility element with an identifier
let myAccessibilityElement = UIAccessibilityElement(accessibilityContainer: self)
// Observe accessibility focus changes
NotificationCenter.default.addObserver(
self,
selector: #selector(accessibilityElementDidLoseFocus),
name: UIAccessibilityElement.didLoseFocusNotification,
object: myAccessibilityElement
)
@objc func accessibilityElementDidLoseFocus() {
// Dismiss keyboard here
}