更改 iOS 8 中的 UISearchBar 取消按钮文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28990854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:09:43  来源:igfitidea点击:

Change UISearchBar cancel button text in iOS 8

iosios8uisearchbaruisearchcontroller

提问by Wilmer

I'd like to change the text from ¨Cancel¨to ¨Done¨ of the Cancel button inside the UISearchBar in iOS 8. I am using UISearchController. I've tried different approaches for iOS 6 and iOS 7 and they do not work. Has anybody done this?

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

回答by Burhanuddin Sunelwala

Objective-C:

目标-C:

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


Swift:

迅速:

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

回答by polo987

This worked for me in ios8, did not try in ios7, but should do the trick, beware of placing this line in the early times of the app cycle (eg : appDelegate)

这在 ios8 中对我有用,在 ios7 中没有尝试,但应该可以解决问题,小心将此行放在应用程序周期的早期(例如:appDelegate)

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

and as of ios9 you could also use

从 ios9 开始,您还可以使用

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

Swift version:

迅捷版:

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

Hope that helps ;)

希望有帮助;)

回答by Wilmer

Found it!. in iOS 8, the cancel button is under the key "cancelButton". I used UISearchBar delegate searchBarTextDidBeginEditing to make the change.

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

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

}

回答by Maysam

Swift 3.0:

斯威夫特 3.0:

In your AppDelegateadd this:

在您的AppDelegate 中添加以下内容:

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

回答by Donal

 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 contain UISearchBarBackground,UISearchBarTextField,UINavigationButto??n in which UINavigationButton is subclass of UIButton.Check for View as UIButton and if yes then change property of UIButton.

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

回答by Vinoth

For iOS 13 Support:

对于 iOS 13 支持:

Swift:

迅速:

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

Objective C:

目标 C:

    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[UISearchBar class]]] setTitle:@"Whatever"]

回答by toddg

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.

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

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

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

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

回答by Aleksejs Mjaliks

In Swift:

在斯威夫特:

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

Based on Burhanuddin Sunelwala answer.

基于 Burhanuddin Sunelwala 的回答。

回答by Akhil Clement

In Swift4

Swift4 中

Change Title:

更改标题:

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

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

Change Color:

换颜色:

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

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

回答by David West

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

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

This goes straight into my ViewDidLoad() {

这直接进入我的 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