iOS Change the title of cancel button in UISearchBar

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

    Refer to @Salih's solution above, I used this code and work perfect! Just call the method setupAppearance in AppDelegate.m when app launching.

    - (void)setupAppearance
    {
       id appearance = nil;
    
       if (IOS9_OR_LATER) {
           appearance = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]];
       } else {
           appearance = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
       }
    
       [appearance setTitle:@"CANCEL"];
    }
    
    0 讨论(0)
  • 2020-12-28 11:06

    For Swift 3.0

    This is working fine.

    func setSearchButtonText(text:String,searchBar:UISearchBar) {
    
        for subview in searchBar.subviews {
            for innerSubViews in subview.subviews {
                if let cancelButton = innerSubViews as? UIButton {
                    cancelButton.setTitleColor(UIColor.white, for: .normal)
                    cancelButton.setTitle(text, for: .normal)
                }
            }
        }
    
    }
    

    And call the method

    setSearchButtonText(text: "Done", searchBar: yourSearchBar)
    

    Here is the output

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

    Chiming in with the code I used inside the UIKitUISearchResultsUpdating protocol method updateSearchResults(for searchController: UISearchController):

    if let cancelButton : UIButton = searchController.searchBar.value(forKey: "_cancelButton") as? UIButton {
        cancelButton.setTitle("Back", for: UIControlState.normal)
    }
    

    (iOS 10, Swift 3.0)

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

    If you just want to change the "Cancel" text to the same in your locale, the proper way I think is using localization. Here is how:

    1. open your project in the Finder, locate the en.lproj directory
    2. rename it to your locale, or duplicate it if you have to support multiple languages
    3. remove any file references it may have contained in Xcode (they should look red)
    4. drag the same files from Finder from the new directory to Xcode
    5. (not sure this is necessary) edit your Info.plist and add these lines:

      • Localization native development region: your region (for example: en)
      • Localizations: for example English

    Note: it does not work in Simulator, test it on the device!

    (Source: http://www.ibabbleon.com/iphone_app_localization.html)

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

    Put this line in appDelegate

    [[UIButton appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"Your Title" forState:UIControlStateNormal];

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

    It seems like "self.searchDisplayController.searchBar.subviews" just return the "searchBar" itself. I tried bellow, and it works!

    for (UIView *searBarView in [self.searchDisplayController.searchBar subviews])
    {
        for (UIView *subView in [searBarView subviews]) 
        {
            if ([subView isKindOfClass:[UIButton class]])
            {
                UIButton *cancleButton = (UIButton *)subView;
                [cancleButton setTitle:@"hi" forState:UIControlStateNormal];
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题