以下のコードを使用しています。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];
}