iOS Change the title of cancel button in UISearchBar

前端 未结 13 1802
隐瞒了意图╮
隐瞒了意图╮ 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 10:50

    in order to make it work on iOS7, you have to search in the subview of search bar:

    //iOS 7 hack
    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:@"Anuluj" forState:UIControlStateNormal];
        }
    }
    
    0 讨论(0)
  • 2020-12-28 11:00

    For iOS7, this works.

    -(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:@"YourTitle" forState:UIControlStateNormal];
        }
    }
    
    0 讨论(0)
  • 2020-12-28 11:01

    simply do this code for it:-

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
        /* when user start editing in serchbar this method will display cancel button and disable the autocorrection functionality */
    
        srcbar.showsCancelButton = YES;
    
        for (UIView *subView in searchBar.subviews) {
            if ([subView isKindOfClass:[UIButton class]]) {
               UIButton *cancelButton = (UIButton*)subView;
    
                [cancelButton setTitle:@"hi" forState:UIControlStateNormal];
            }
        }
        srcbar.autocorrectionType = UITextAutocorrectionTypeNo;
    
    }
    

    Not test in iOS7 but this working fine in iOS6 hope this working for you.

    OUTPUT IS:-

    enter image description here

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-28 11:02

    here is my solution for both ios6 & ios7

    #define IS_IOS7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        self.searchDisplayController.searchBar.showsCancelButton = YES;
    
        UISearchBar *searchBar = self.searchDisplayController.searchBar;
        UIView *viewTop = IS_IOS7 ? searchBar.subviews[0] : searchBar;
        NSString *classString = IS_IOS7 ? @"UINavigationButton" : @"UIButton";
    
        for (UIView *subView in viewTop.subviews) {
            if ([subView isKindOfClass:NSClassFromString(classString)]) {
                UIButton *cancelButton = (UIButton*)subView;
                [cancelButton setTitle:@"your title" forState:UIControlStateNormal];
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-28 11:03

    @Nitin Gohel Like so?

    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
      self.searchDisplayController.searchBar.showsCancelButton = YES;
      UIButton *cancelButton = nil;
      for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
         if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
             cancelButton = (UIButton*)subView;
    
             [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];
         }
     }
    
    }
    
    0 讨论(0)
提交回复
热议问题