iOS Change the title of cancel button in UISearchBar

前端 未结 13 1890
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 10:35

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

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayCon         


        
13条回答
  •  天涯浪人
    2020-12-28 11:02

    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.

提交回复
热议问题