尝试覆盖UISearchBar的resignFirstResponder方法,以便始终启用“取消”按钮:
@interface UISearchBar(alwaysEnableCancelButton)
- (BOOL)resignFirstResponder;
@end
@implementation UISearchBar(alwaysEnableCancelButton)
- (BOOL)resignFirstResponder
{
// Force the cancel button to stay enabled
UIView *possibleButton;
for (possibleButton in self.subviews)
{
if ([possibleButton isKindOfClass:[UIButton class]])
{
UIButton *cancelButton = (UIButton*)possibleButton;
[cancelButton setEnabled:YES];
}
// Dismiss the keyboard
if ([possibleButton isKindOfClass:[UITextField class]]) {
[(UITextField *)possibleButton resignFirstResponder];
}
}
NSLog(@"done");
return [super resignFirstResponder];
}
@end
但是日志没有显示!
我正在使用它
[mySearchBar resignFirstResponder];