Rameez Hussain
Rameez Hussain

Reputation: 6764

Customising UISearchBar in iOS 7

I have a UISearchBar whose appearance I want to customise. The suggestion in this post worked before the update to iOS 7. But now I'm not sure how to do it. I mainly want to customise the Cancel button. Does anybody know how?

Upvotes: 1

Views: 1002

Answers (1)

Guy Kogus
Guy Kogus

Reputation: 7351

You need to search for the button recursively. This should be a fail-safe way to do it:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self convertButtonTitle:@"Cancel" toTitle:@"Annuller" inView:self.searchBar];
}

- (void)convertButtonTitle:(NSString *)from toTitle:(NSString *)to inView:(UIView *)view
{
    if ([view isKindOfClass:[UIButton class]])
    {
        UIButton *button = (UIButton *)view;
        if ([[button titleForState:UIControlStateNormal] isEqualToString:from])
        {
            [button setTitle:to forState:UIControlStateNormal];
        }
    }

    for (UIView *subview in view.subviews)
    {
        [self convertButtonTitle:from toTitle:to inView:subview];
    }
}

I've tested this on iOS 7 only, but it works and should do so for iOS 6 too.

Upvotes: 2

Related Questions