我正在使用下面的代码。它在 IOS 6.x 中运行良好。从 Xcode 4.6.x 构建。但是该代码在从 xcode 5.x 构建的 IOS 7.x 中不再起作用。
我的目标是在用户开始在 UISearchBar 中编辑时显示自定义图像来代替 UISearchBar 的取消按钮。
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
UIButton *cancelButton = nil;
for(UIView *subView in searchBar.subviews){
if([subView isKindOfClass:UIButton.class]){
cancelButton = (UIButton*)subView;
}
}
[cancelButton setTintColor:[UIColor colorWithRed:167.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0f]];
[cancelButton setBackgroundImage:[UIImage imageNamed:@"customKeyboardIcon.png"] forState:UIControlStateNormal];
[cancelButton setTitle:nil forState:UIControlStateNormal];
}