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

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

    In iOS 7 if you are using UISearchBar just write this code in searchBarTextDidBeginEditing: method

    searchBar.showsCancelButton = YES;UIView* view=searchBar.subviews[0];
    for (UIView *subView in view.subviews) {
          if ([subView isKindOfClass:[UIButton class]]) {
              UIButton *cancelButton = (UIButton*)subView;
    
              [cancelButton setTitle:@"إلغاء" forState:UIControlStateNormal];
           }
    }
    
    0 讨论(0)
  • 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]; 
    }
    
    0 讨论(0)
  • 2020-12-01 03:36

    You also need to have the "searchBar setShowsCancelButton" before the procedure.

    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        [theSearchBar setShowsCancelButton:YES animated:NO];
        for (UIView *subView in theSearchBar.subviews){
            if([subView isKindOfClass:[UIButton class]]){
                [(UIButton*)subView setTitle:@"Done" forState:UIControlStateNormal];
            }
        }
    }
    

    Note also: use UIButton to avoid problems with Apple!

    0 讨论(0)
  • 2020-12-01 03:36

    If by "Search String", you mean the placeholder, then this should do it:

    [searchBar setPlaceholder:@"Whatever you want"];
    

    As for changing the text of the cancel button, that may be a bit more difficult. Apple does not use a standard UIBarButtonItem for this, or even a non-standard UIButton. Instead they use a UINavigationButton for the cancel button in the search bar. Since this is not a documented public class, attempting to modify it could very well get your app rejected from the App Store. If you do want to risk rejection, then you could search through the subviews of searchBar:

    for(UIView *view in [searchBar subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            [(UIBarItem *)view setTitle:@"Whatever you want"];
        }
    }
    

    Note that the cancel button is loaded lazily, so you will have to do this modification when the search bar is activated by the user.

    0 讨论(0)
  • 2020-12-01 03:37

    Jeremytripp 's working Code in Swift

    I couldn't find the same code in Swift so I "translated" it myself:

    func searchDisplayControllerWillBeginSearch(controller: UISearchDisplayController) {
        self.searchDisplayController?.searchBar.showsCancelButton = true
        var cancelButton: UIButton
        var topView: UIView = self.searchDisplayController?.searchBar.subviews[0] as UIView
        for subView in topView.subviews {
            if subView.isKindOfClass(NSClassFromString("UINavigationButton")) {
            cancelButton = subView as UIButton
            cancelButton.setTitle("My Custom Title", forState: UIControlState.Normal)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 03:41

    If you're still having trouble with changing the Cancel button in iOS7, this is currently working for me:

    -(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
        self.searchDisplayController.searchBar.showsCancelButton = YES;
        UIButton *cancelButton;
        UIView *topView = self.searchDisplayController.searchBar.subviews[0];
        for (UIView *subView in topView.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
                cancelButton = (UIButton*)subView;
            }
        }
        if (cancelButton) {
          //Set the new title of the cancel button
            [cancelButton setTitle:@"Hi" forState:UIControlStateNormal];
        }
    }
    
    0 讨论(0)
提交回复
热议问题