Enable cancel button with UISearchBar in iOS8

Is there a way to enable the Cancel button in a UISearchBar? Now when I call resignFirst responder, the cancel button is disabled. Only when I click on the search bar again, cancel is activated. Is there any way to stop the cancel button cancel?

+4
iphone cocoa-touch ios8 uisearchbar
Nov 19 '14 at 15:20
source share
3 answers

Here is a working solution for iOS 8 and Swift.

func enableCancleButton (searchBar : UISearchBar) { for view1 in searchBar.subviews { for view2 in view1.subviews { if view2.isKindOfClass(UIButton) { var button = view2 as! UIButton button.enabled = true button.userInteractionEnabled = true } } } } 
+7
Apr 08 '15 at 8:52
source share

For iOS7:

 - (void)enableCancelButton{ for (UIView *view in self.subviews){ for (id subview in view.subviews){ if ([subview isKindOfClass:[UIButton class]]){ [subview setEnabled:YES]; return; } } } } 

To make the code higher in iOS8, you need to add a delay before enabling subview:

 - (void)enableCancelButton{ for (UIView *view in self.subviews){ for (id subview in view.subviews){ if ([subview isKindOfClass:[UIButton class]]){ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10), dispatch_get_main_queue(), ^{ [subview setEnabled:YES]; }); return; } } } } 
+4
Jan 22 '15 at 5:18
source share
 UIBarButtonItem.appearance().enabled = true 

did the trick for me

0
Feb 24 '16 at 10:51
source share



All Articles