I know this one is old, but I was able to solve it, and I could not find anything else in SO that solved it for me, so this worked (in Swift 3):
Enable the cancel button when hiding the keyboard. Add this to viewDidLoad ():
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
in the keyboardNotification (notification :) method, respond to the keyboard notification DidHide:
func keyboardNotification(notification: NSNotification) { if notification.name == Notification.Name.UIKeyboardDidHide { self.enableSearchCancelButton() } }
The enableSearchCancelButton button is taken from what others answered here .
func enableSearchCancelButton() { //enable the cancel button for view1 in searchBar.subviews { for view2 in view1.subviews { if let button = view2 as? UIButton { button.isEnabled = true button.isUserInteractionEnabled = true } } } }
Finally, remember to remove the view controller as an observer:
deinit { NotificationCenter.default.removeObserver(self) }
Yariv Adam Feb 09 '17 at 10:20 2017-02-09 10:20
source share