Danienllxxox Danienllxxox - 8 months ago 66
iOS Question

iOS Change the title of cancel button in UISearchBar

I wish to change the title of the cancel button in iOS. I have been using this previously:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIButton *cancelButton = nil;
for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
cancelButton = (UIButton*)subView;
[cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];

But it doesnt seem to work in iOS7. Any Suggestions?


simply do this code for it:-

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    /* when user start editing in serchbar this method will display cancel button and disable the autocorrection functionality */

    srcbar.showsCancelButton = YES;

    for (UIView *subView in searchBar.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
           UIButton *cancelButton = (UIButton*)subView;

            [cancelButton setTitle:@"hi" forState:UIControlStateNormal];
    srcbar.autocorrectionType = UITextAutocorrectionTypeNo;


Not test in iOS7 but this working fine in iOS6 hope this working for you.


enter image description here