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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
}
}
}
}
回答2:
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;
}
}
}
}
回答3:
UIBarButtonItem.appearance().enabled = true
did the trick for me