Using a UISearchBar with showCancelButton=YES on iOS 5. Would like the cancel button to stay enabled when the keyboard drops down. Using the following code seems not to work:
for (id subView in self.searchControl.subviews)
{
if ([subView isKindOfClass:[UIButton class]])
{
UIButton *cancelButton = (UIButton *)subView;
[cancelButton setEnabled:YES];
break;
}
}
The subView is actually a UINavigationButton which appears not to be subclassed off of UIButton. What am I missing here??????? Also cannot find any info on the UINavigationButton class in the Apple docs.
Swift 4:
I had to solve the same problem in my application. Try doing your above code after a fractional delay, e.g.
It's ugly, but that's what it took to work for me. Alternatively, if you actually use a UISearchDisplayController to display the results, it should also fix the cancel button behavior for you (I think - I've delved into this issue less).
I placed a custom cancel button over the search bar cancel button.
Just to improve upon what Kurt Spindler said on his post. Though this might not be superior but it is more contained. I use dispatch to do the same thing.
This should work for everyone who needs help keep cancel enabled. Make sure that you hide it later either with the cancel or Search clicked.
I know this one is old, but I was able to solve it and I couldn't find anything else on SO that solved it for me, so here's what worked (in Swift 3):
Enable the cancel button when the keyboard hides. Add this to viewDidLoad():
in the keyboardNotification(notification:) method, react to the keyboardDidHide notification:
The enableSearchCancelButton is taken from what others have answered here.
Finally, don't forget to remove the view controller as an observer:
Set your searchbar delegate and than put this code.
Swift 3.0