Swift:searchBar - 如何更改"取消"按钮标题

时间:2015-04-04 11:21:56

标签: ios swift uisearchbar

我有UIViewController,它采用了UISearchBarDelegate。并使用以下命令将其委托设置为self:resultSearchController.searchBar.delegate = self。它运行正常我用searchBarCancelButtonClicked方法%

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

我在控制台中看到“Сancel按钮轻敲”。但我想改变“取消”按钮标题,我不知道如何。我尝试过:

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
    }

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

7 个答案:

答案 0 :(得分:4)

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

Swift 4

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

它对我有用:)

答案 1 :(得分:2)

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)
        }
    }
}

答案 2 :(得分:0)

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

  

在ViewDidLoad()

中      

self.searchDisplayController?.displaysSearchBarInNavigationBar = true

并在Delegate Method

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

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

答案 3 :(得分:0)

我通过在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;
}

答案 4 :(得分:0)

选中此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>)
}

答案 5 :(得分:0)

在初始化搜索控制器后,将其放入视图控制器的viewDidLoad()中。

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

答案 6 :(得分:0)

对于 Swift 5

searchBar.delegate = self enter image description here

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