Danienllxxox Danienllxxox - 1 month ago 20
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?

Answer

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.

OUTPUT IS:-

enter image description here