Change UISearchBar cancel button text in iOS 8

后端 未结 13 1587
感动是毒
感动是毒 2020-12-13 01:48

I\'d like to change the text from "Cancel" to "Done" of the Cancel button inside the UISearchBar in iOS 8. I am using UISearchControll

相关标签:
13条回答
  • 2020-12-13 02:37

    In Swift:

    searchBar.setValue("customString", forKey: "_cancelButtonText")
    

    Based on Burhanuddin Sunelwala answer.

    0 讨论(0)
  • 2020-12-13 02:38

    I have added titleLabel.font to the mix...

    This goes straight into my ViewDidLoad() {

            let searchFont = UIFont(name: "BrandonGrotesque-Medium", size: 12)
            let textFieldSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
            textFieldSearchBar.font = searchFont
            let cancelButton = searchBar.valueForKey("cancelButton") as! UIButton
            cancelButton.setTitle("Done", forState: .Normal)
            cancelButton.titleLabel!.font = searchFont
    
    0 讨论(0)
  • 2020-12-13 02:40

    Objective-C:

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

    Swift:

    searchBar.setValue("customString", forKey:"_cancelButtonText")
    
    0 讨论(0)
  • 2020-12-13 02:41

    Here is Swift solution:

    for (view) in _searchController.searchBar.subviews {
        for (subview) in view.subviews {
            if (subview.isKindOfClass(UIButton.self)) {
                let cancelButton = subview as! UIButton
                cancelButton.setTitle("BlaBla", forState: .Normal)
                break
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 02:44

    Swift 3.0:

    In your AppDelegate add this:

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "my text"
    
    0 讨论(0)
  • 2020-12-13 02:47

    I was having trouble getting this to work for a UISearchBar within a UISearchController. The text didn't change the first time the cancel button was shown but it did the second time.

    That is until I saw @polo987's answer. Here's what I did that worked:

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Done"
    
    0 讨论(0)
提交回复
热议问题