【问题标题】:UISearchController change 'Cancel' button titleUISearchController 更改“取消”按钮标题
【发布时间】:2015-02-21 06:16:43
【问题描述】:

我们如何更改搜索控制器中取消按钮的标题?

【问题讨论】:

标签: ios uitableview uisearchcontroller


【解决方案1】:

上面提供的解决方案可能会随着新的 iOS 版本而改变。这是一个更好的方法:

[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];

【讨论】:

  • _cancelButtonText 是私有的,如果 Apple 决定重命名它,它也可能会随着新的 iOS 版本而改变,并且由于它是私有的,他们可以放心地假设没有人在他们的应用程序中使用这个名称
  • 我发现这个值也只能更改一次。我可以动态更改文本(语言)的任何其他解决方案?
  • @JoshuaVidamo 不只是一次,每当您调用上述方法时,它都会被更改。你的问题到底是什么?
  • 在iOS 13 SDK上构建并且应用在iOS 13上运行时会引发异常。有更新的解决方案吗?
  • @user102008 使用上面提到的外观解决方案。
【解决方案2】:

相当于 Burhanuddin Sunelwala 的 Swift 答案对我有用!

self.searchBar.setValue("custom string", forKey: "cancelButtonText")

感谢 Burhanuddin Sunelwala 为我指明了正确的方向!

【讨论】:

    【解决方案3】:

    您可以使用此更改搜索栏中的“取消”按钮-

    for (UIView *view in searchBar.subviews)
    {
        for (id subview in view.subviews)
        {
            if ( [subview isKindOfClass:[UIButton class]] )
            {
                [subview setEnabled:YES];
                UIButton *cancelButton = (UIButton*)subview;
                [cancelButton setTitle:@"hi" forState:UIControlStateNormal];
    
    
                NSLog(@"enableCancelButton");
                return;
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      值得注意的是,现在更改“取消”按钮标题的首选方法是通过外观(从另一个问题得到这个想法:https://stackoverflow.com/a/34522163/511878):

      [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"Annuler"];
      

      【讨论】:

        【解决方案5】:

        您应该使用 UISearchBar 的外观代理。你可以在这里找到一个例子 - How to change the default text of Cancel Button which appears in the UISearchBar +iPhone

        【讨论】:

          【解决方案6】:

          快速版本:

          for view:UIView in (searchView?.subviews)!
          {
                  for subView:UIView in (view.subviews)
                  {
                      if ( subView is UIButton )
                      {
                          let cancelBut = subView as! UIButton
          
                          //do stuff with cancelButton here
                      }
                  }
              }
          

          【讨论】:

            【解决方案7】:
            for (UIView *subView in SearchBar.subviews) {
                if ([subView isKindOfClass:[UIButton class]]) {
                    UIButton *cancelButton = (UIButton*)subView;
            
                    [cancelButton setTitle:@"TitleString" forState:UIControlStateNormal];
            }
            

            【讨论】:

              【解决方案8】:
                class ViewController: UIViewController,UISearchResultsUpdating {
                      func updateSearchResults(for searchController: UISearchController) {
                          //write your code here
                      }
                      @IBOutlet weak var navItem: UINavigationItem!
                      let searchController = UISearchController(searchResultsController: nil)
                      override func viewDidLoad() {
                          super.viewDidLoad()
                          searchController.obscuresBackgroundDuringPresentation = false
                          searchController.definesPresentationContext = true
                          searchController.searchResultsUpdater = self
                          if #available(iOS 11.0, *){
                              navigationItem.searchController = searchController
                              UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "new title"
                          }
                          // Do any additional setup after loading the view.
                      }
                  }
              

              【讨论】:

                【解决方案9】:

                swift 4 & 5

                最简单对我有用的方法是这个 sn-p 代码:

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

                您可以像这样修改更多属性:

                if let cancelButton = searchBar.value(forKey: "cancelButton") as? UIButton {
                    cancelButton.setTitle(<your_string>, for: <UIControlState>)
                    cancelButton.setTitleColor(<your_uicolor>, for: <UIControlState>)
                    cancelButton.setAttributedTitle(<your_nsattributedstring>, for: <UIControlState>)
                }
                

                我希望这会有所帮助:)

                【讨论】:

                  【解决方案10】:

                  您还需要在程序之前拥有searchBar setShowsCancelButton

                  - (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];
                          }
                      }
                  }
                  

                  注意也要使用 UIButton 来避免 Apple 的问题!

                  【讨论】:

                    【解决方案11】:

                    斯威夫特4.2、4.0+

                    在答案here 中添加了支持许多自定义的自定义class,结果如下。

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多