UISearchBar Cancel Button

Using UISearchBar with showCancelButton = YES on iOS 5. It is advisable that the cancel button stay on when the keyboard crashes. Using the following code does not seem to work:

for (id subView in self.searchControl.subviews) { if ([subView isKindOfClass:[UIButton class]]) { UIButton *cancelButton = (UIButton *)subView; [cancelButton setEnabled:YES]; break; } } 

SubView is actually a UINavigationButton that does not appear to be subclassed with UIButton. What am I missing here ??????? Also, you cannot find information about the UINavigationButton class in Apple docs.

+10
uisearchbar
Dec 16 '11 at 15:50
source share
6 answers

Set your delegate in the search bar, and then put this code.

 - (void) searchBarSearchButtonClicked:(UISearchBar*) theSearchBar { [theSearchBar resignFirstResponder]; [theSearchBar setShowsCancelButton:NO animated:YES]; } - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { [searchBar setShowsCancelButton:YES animated:YES]; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; [searchBar setShowsCancelButton:NO animated:YES]; } 

Swift 3.0

 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { searchBar.resignFirstResponder() searchBar.setShowsCancelButton(false, animated: true) } func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { searchBar.setShowsCancelButton(true, animated: true) } func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { searchBar.resignFirstResponder() searchBar.setShowsCancelButton(false, animated: true) } 
+21
Mar 20 '14 at 14:12
source share

Just to improve what Kurt Spindler said at his post. Although this may not be excellent, but more substantial. I use sending to do the same.

 -(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{ for (UIView *subview in searchBar.subviews) { if ([subview isKindOfClass:[UIButton class]]) { int64_t delayInSeconds = .001; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ UIButton * cancelButton = (UIButton *)subview; [cancelButton setEnabled:YES]; }); break; } } } 

This should work for anyone who needs help to cancel a cancellation. Make sure you hide it later with either cancellation or search.

 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ [searchBar resignFirstResponder]; [searchBar setShowsCancelButton:NO animated:YES]; } 
+2
Oct 17
source share

I had to solve the same problem in my application. Try to execute your code above after fractional delay, for example.

 [self performSelector:@selector(delayedEnable:) withObject:cancelButton afterDelay:0.001]; - (void)delayedEnable:(UIButton*)button { button.enabled = YES; } 

It is ugly, but it is what I need to work for me. Alternatively, if you really use the UISearchDisplayController to display the results, it should also fix the cancel button behavior for you (I think I delved into this problem less).

+1
Dec 28 '11 at 20:56
source share

I placed a custom cancel button on the cancel button of the search bar.

+1
Jan 08 '13 at 6:27
source share

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) } 
+1
Feb 09 '17 at 10:20
source share

Swift 5 :, Swift 4:

 if let btnCancel = searchBar.value(forKey: "cancelButton") as? UIButton { btnCancel.isEnabled = true } 
+1
Feb 05 '19 at 11:14
source share



All Articles