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

后端 未结 15 1979
慢半拍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:34

    This solution work for me - iOs7 and iOs8:

    @interface ... : ...
    @property (strong, nonatomic) IBOutlet UISearchBar *search; 
    @end
    

    and

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
    
    NSArray *searchBarSubViews = [[self.search.subviews objectAtIndex:0] subviews];
    UIButton *cancelButton;
    for (UIView *subView in searchBarSubViews) {
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            cancelButton = (UIButton*)subView;
            break;
        }
    }
    if (cancelButton) {
    
        [cancelButton setTitle:@"New cancel" forState:UIControlStateNormal];
    
    }
     //insert this two lines below if you have a button appearance like this "Ne...cel" 
    
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar setShowsCancelButton:YES animated:YES]; 
    }
    

提交回复
热议问题