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

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

    if the SearchBar is in the navigationBar, the code will be different than the usual answer; You need to search for NavigationBar's subviews instead.

    -(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
    UINavigationBar * navigationBar =  self.navigationController.navigationBar;
    
    for (UIView *subView in navigationBar.subviews){
        if([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]){
            [(UIButton*)subView setTitle:@"İptal" forState:UIControlStateNormal];
        }
    }}
    

    and This work in iOS7+ , if you still can't set the title you should learn view debugging - This is how I solved this problem of mine.

    This brief tutorial outlines the key points of View-Debugging very well:

    http://www.raywenderlich.com/98356/view-debugging-in-xcode-6

    0 讨论(0)
  • 2020-12-01 03:43
        if #available(iOS 13.0, *) {
            controller.searchBar.setValue("Done", forKey:"cancelButtonText")
        } else {
            controller.searchBar.setValue("Done", forKey:"_cancelButtonText")
        }
    

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

    If you just want to localized the default "Cancel" title for cancel button, I prefer to change the value of CFBundleDevelopmentRegion key from en to your localized region in Info.plist file in project.

    Here is my change,

    <key>CFBundleDevelopmentRegion</key>
    <string>zh_CN</string>
    

    after that, the default "Cancel" title will show as Chinese "取消". This change will also affect all the default region values, for example, the pasteboard operations' action titles on UITextField/UITextView will be localized, "Select" -> "选择", "Paste" -> "粘贴"...

    By the way, the Info.plist file could be localized perfectly.

    Enjoy!

    0 讨论(0)
提交回复
热议问题