0

I'm having trouble changing the text of the searchbar cancel button.

See my code:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [_srBar setShowsCancelButton:YES animated:YES];
    UIButton *cancelButton = nil;
    for (UIView *subView in searchBar.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
            cancelButton = (UIButton*)subView;
        }
    }
    [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal]; 
}

Also I already tried other options

iOS Change the title of cancel button in UISearchBar iOS - Customizing Cancel button of UISearchBar

In this example my code does not call the method

How to change the default text of Cancel Button which appears in the UISearchBar +iPhone

my scenery

#import <UIKit/UIKit.h>

@interface favorito : UIViewController<UISearchControllerDelegate, UISearchDisplayDelegate, UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>

@end
Community
  • 1
  • 1
leedream
  • 559
  • 1
  • 6
  • 21

1 Answers1

1
id barButtonAppearanceInSearchBar = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];

[barButtonAppearanceInSearchBar setBackgroundImage:grayBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[barButtonAppearanceInSearchBar setTitleTextAttributes:@{
                                      UITextAttributeFont : [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:20],
                                 UITextAttributeTextColor : [UIColor blackColor]
     } forState:UIControlStateNormal];
[barButtonAppearanceInSearchBar setTitle:@"X"];
Mubin Mall
  • 546
  • 10
  • 25