【问题标题】:Swift: searchBar - how to change "Cancel" Button titleSwift:searchBar - 如何更改“取消”按钮标题
【发布时间】:2015-04-04 11:21:55
【问题描述】:

我有采用 UISearchBarDelegate 的 UIViewController。并将其委托设置为 self:resultSearchController.searchBar.delegate = self。它工作正常我用 searchBarCancelButtonClicked 方法测试它%

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        println("Сancel button tapped")
    }

我在控制台中看到“Сancel button tapped”。但我想更改“取消”按钮标题,但我不知道怎么做。我试过了:

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {      
        var barButton = UIBarButtonItem(title: "Button Title", style: UIBarButtonItemStyle.Done, target: self, action: "here")
        self.resultSearchController.searchBar.showsCancelButton = false
        self.resultSearchController.navigationItem.rightBarButtonItem = barButton
    }

但它不起作用。你能帮帮我吗?

【问题讨论】:

    标签: ios swift uisearchbar


    【解决方案1】:

    尝试使用 setValue 访问按钮文本,如下所示:

    斯威夫特 4

    searchController.searchBar.setValue("New Title", forKey: "cancelButtonText")
    

    它对我有用:)

    【讨论】:

    • 这在 Swift 4 中有效,谢谢!这比公认的答案容易得多
    【解决方案2】:

    初始化你的搜索控制器后,把它放在视图控制器的viewDidLoad()中。

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "your button title"
    

    【讨论】:

      【解决方案3】:
      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("Vazgeç", forState: UIControlState.Normal)
              }
          }
      }
      

      【讨论】:

      • 我不使用 UISearchDisplayController 它已被弃用。我使用 UISearchController。所以这段代码帮不了我。
      【解决方案4】:

      对于 Swift 5

      searchBar.delegate = self

      func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
           let cBtn = searchBar.value(forKey: "cancelButton") as! UIButton
           cBtn.setTitle("MyCustomText", for: .normal)
      }
      

      【讨论】:

        【解决方案5】:

        检查这个answer

        上面的答案示例针对 Swift 5 更新:

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

        【讨论】:

        • 您的回答对我有帮助!但是我怎样才能解决同样的问题到书签按钮?
        【解决方案6】:

        首先你需要在导航控制器中显示自定义搜索栏按钮

        在 ViewDidLoad() 中

        self.searchDisplayController?.displaysSearchBarInNavigationBar = true

        在委托方法中

        func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        
            var barButton = UIBarButtonItem(title: "Button Title", style: UIBarButtonItemStyle.Done, target: self, action: "here")
            self.searchDisplayController?.navigationItem.rightBarButtonItem = barButton
        }
        

        【讨论】:

        • 我试过你的代码。没有任何改变 - 我看到搜索栏带有“取消”,抱歉,我可能错过了重点……请给我更多信息。
        • 拜托,我已经在这几个小时内伤透了脑筋......我不使用 UISearchDisplayController 它已被弃用。我使用 UISearchController。
        【解决方案7】:

        我通过在 UISearchBar 对象中找到 UIButton 指针解决了这个问题。我的项目中有 Objective-c 代码,但你可以看到逻辑。

        - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
        
            [searchBar setShowsCancelButton:YES animated:YES];
        
            for (UIView *ob in ((UIView*)searchBar.subviews[0]).subviews) {
                if ([ob isKindOfClass:[UIButton class]]) {
                    UIButton *btn = (UIButton*)ob;
                    [btn setTitle:@"İptal" forState:UIControlStateNormal];
                }
            }
            return YES;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-12
          • 1970-01-01
          • 2016-09-22
          相关资源
          最近更新 更多