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

后端 未结 15 2015
慢半拍i
慢半拍i 2020-12-01 03:04

I am developing an Application where I wanted to change the text of Search String in the SearchBar. I wanted to change the text of Cancel Button Also which appears next to t

15条回答
  •  心在旅途
    2020-12-01 03:26

    On iOS 7, if you've set displaysSearchBarInNavigationBar = YES on UISearchDisplayController, replacing the cancel button title via subview recursion or the appearance proxy will not work.

    Instead, use your own bar button in viewDidLoad:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
        UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"A Custom Title", nil)
                                                                    style:UIBarButtonItemStyleBordered
                                                                   target:self
                                                                   action:@selector(cancelButtonTapped:)];
    
        // NB: Order is important here.
        //     Only do this *after* setting displaysSearchBarInNavigationBar to YES
        //     as that's when UISearchDisplayController creates it's navigationItem
        self.searchDisplayController.navigationItem.rightBarButtonItem = barItem;
    }
    

提交回复
热议问题