Skip to content

Instantly share code, notes, and snippets.

@pruinis
Forked from jeksys/UISearchBar.m
Created December 30, 2015 12:05
Show Gist options
  • Select an option

  • Save pruinis/5d99f947f80af83407cd to your computer and use it in GitHub Desktop.

Select an option

Save pruinis/5d99f947f80af83407cd to your computer and use it in GitHub Desktop.
Detecting when clear is clicked in UISearchBar (X button)
- (void)viewDidLoad {
//find the UITextField view within searchBar (outlet to UISearchBar)
//and assign self as delegate
for (UIView *view in searchBar.subviews){
if ([view isKindOfClass: [UITextField class]]) {
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) aSearchBar {
[aSearchBar resignFirstResponder];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
//if we only try and resignFirstResponder on textField or searchBar,
//the keyboard will not dissapear (at least not on iPad)!
[self performSelector:@selector(searchBarCancelButtonClicked:) withObject:self.searchBar afterDelay: 0.1];
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment