Is there any method to enable 'Cancel' button of UISearchBar? Now whenever I call resignFirst responder, cancel button gets disabled. Only when I tap over the search bar again, cancel gets enabled. Is there anyway to stop disabling the cancel button?
Here's 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
}
}
}
}
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 above code work in iOS8, you need add a delay before enable the 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;
}
}
}
}
UIBarButtonItem.appearance().enabled = true
did the trick for me
来源:https://stackoverflow.com/questions/27020452/enable-cancel-button-with-uisearchbar-in-ios8