44

我想将UISearchBariOS 8 中“取消”按钮的文本从“取消”更改为“完成”。我正在使用UISearchController. 我为 iOS 6 和 iOS 7 尝试了不同的方法,但它们不起作用。有人做过吗?

4

14 回答 14

88

目标-C:

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

迅速:

searchBar.setValue("customString", forKey:"_cancelButtonText")
于 2015-03-25T10:51:53.883 回答
38

这在 ios8 到 ios13 中对我有用,在 ios7 中没有尝试过,但应该可以解决问题,注意不要将这一行放在应用程序周期的早期(例如:appDelegate)

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"Annuler"];

从 ios9 开始,您还可以使用

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

斯威夫特版本:

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

希望有帮助;)

于 2015-12-30T02:32:16.410 回答
20

找到了!。在 iOS 8 中,取消按钮位于“cancelButton”键下方。我使用 UISearchBar 委托 searchBarTextDidBeginEditing 进行更改。

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    let uiButton = searchBar.valueForKey("cancelButton") as UIButton
    uiButton.setTitle("Done", forState: UIControlState.Normal)

}
于 2015-03-24T12:05:16.823 回答
14

斯威夫特 3.0:

在您的AppDelegate中添加:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "my text"
于 2017-02-12T10:11:44.127 回答
9
 for view in (UISearchBar.subviews[0]).subviews{
             if let button = view as? UIButton{
             button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
             button.setTitle("Cancel", forState:.Normal)
            }
     }

self.subviews[0]).subviews 包含 UISearchBarBackground,UISearchBarTextField,UINavigationButto‌​n 其中 UINavigationButton 是 UIButton 的子类。检查 View as UIButton 如果是,则更改 UIButton 的属性。

于 2016-10-28T12:52:13.307 回答
8

我无法让它在 UISearchController 中为 UISearchBar 工作。第一次显示取消按钮时,文本没有更改,但第二次显示了。

直到我看到@polo987 的回答。这是我所做的工作:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Done"
于 2016-05-10T22:14:31.290 回答
8

对于 iOS 13 支持:

迅速:

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

目标 C:

    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[UISearchBar class]]] setTitle:@"Whatever"]
于 2019-10-17T08:34:49.120 回答
6

在斯威夫特:

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

基于 Burhanuddin Sunelwala 的回答。

于 2015-09-24T08:36:54.510 回答
6

Swift4 中

更改标题:

(searchBar.value(forKey: "cancelButton") as! UIButton).setTitle("Done", for: .normal)

换颜色:

(searchBar.value(forKey: "cancelButton") as! UIButton).setTitleColor(UIColor.blue, for: .normal)

于 2017-10-25T15:45:45.077 回答
3

我已将 titleLabel.font 添加到组合中...

这直接进入我的 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
于 2016-04-07T04:32:19.313 回答
3

斯威夫特 4.2、4.0+

一个自定义类,用于自定义搜索栏中最常见的元素。

class SearchBar: UISearchBar {

    private enum SubviewKey: String {
        case searchField, clearButton, cancelButton,  placeholderLabel
    }

    // Button/Icon images
    public var clearButtonImage: UIImage?
    public var resultsButtonImage: UIImage?
    public var searchImage: UIImage?

    // Button/Icon colors
    public var searchIconColor: UIColor?
    public var clearButtonColor: UIColor?
    public var cancelButtonColor: UIColor?
    public var capabilityButtonColor: UIColor?

    // Text
    public var textColor: UIColor?
    public var placeholderColor: UIColor?
    public var cancelTitle: String?

    // Cancel button to change the appearance.
    public var cancelButton: UIButton? {
        guard showsCancelButton else { return nil }
        return self.value(forKey: SubviewKey.cancelButton.rawValue) as? UIButton
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        if let cancelColor = cancelButtonColor {
            self.cancelButton?.setTitleColor(cancelColor, for: .normal)
        }
        if let cancelTitle = cancelTitle {
            self.cancelButton?.setTitle(cancelTitle, for: .normal)
        }

        guard let textField = self.value(forKey: SubviewKey.searchField.rawValue) as? UITextField else { return }

        if let clearButton = textField.value(forKey: SubviewKey.clearButton.rawValue) as? UIButton {
            update(button: clearButton, image: clearButtonImage, color: clearButtonColor)
        }
        if let resultsButton = textField.rightView as? UIButton {
            update(button: resultsButton, image: resultsButtonImage, color: capabilityButtonColor)
        }
        if let searchView = textField.leftView as? UIImageView {
            searchView.image = (searchImage ?? searchView.image)?.withRenderingMode(.alwaysTemplate)
            if let color = searchIconColor {
                searchView.tintColor = color
            }
        }
        if let placeholderLabel =  textField.value(forKey: SubviewKey.placeholderLabel.rawValue) as? UILabel,
            let color = placeholderColor {
            placeholderLabel.textColor = color
        }
        if let textColor = textColor  {
            textField.textColor = textColor
        }
    }

    private func update(button: UIButton, image: UIImage?, color: UIColor?) {
        let image = (image ?? button.currentImage)?.withRenderingMode(.alwaysTemplate)
        button.setImage(image, for: .normal)
        button.setImage(image, for: .highlighted)
        if let color = color {
            button.tintColor = color
        }
    }
}

用法:

class ViewController: UIViewController {

    @IBOutlet private weak var searchBar: SearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.clearButtonColor      = .purple
        searchBar.cancelButtonColor     = .magenta
        searchBar.searchIconColor       = .red
        searchBar.placeholderColor      = .green
        searchBar.textColor             = .orange
        searchBar.capabilityButtonColor = .green
    }
}

结果: 在此处输入图像描述

于 2018-07-16T06:20:06.930 回答
2

我知道这似乎有点无关紧要,但在我看来,这比使用私有 api 更安全,也比潜入未知视图更有效。仅当您拥有自己的本地化引擎并且确实希望 Apple 在整个应用程序中遵循您的机制时,此解决方案才有意义。基本上我的想法是通过更改 NSUserDefaults 中的“AppleLanguages”键来切换 iOS SDK 用于本地化按钮的语言。您可以在此处了解有关其工作原理的更多信息。

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

无论手机上设置什么语言,都可以使用此代码使用英语本地化和法语后备。这仅在应用程序内生效。

于 2016-08-23T14:35:34.810 回答
1

这是斯威夫特的解决方案:

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
        }
    }
}
于 2015-11-24T12:11:35.290 回答
-1

让 uiButton = searchBar.value(forKey: "cancelButton") 为!UIButton uiButton.setTitle(NSLocalizedString("Cancel", comment: ""), for: .normal)

于 2021-06-16T12:16:03.197 回答