我想在iOS 8中的UISearchBar中的“取消”按钮的“取消”中更改文本。我正在使用UISearchController。我尝试过iOS 6和iOS 7的不同方法,但它们不起作用。有人这样做过吗?
答案 0 :(得分:80)
<强>目标-C:强>
[searchBar setValue:@"customString" forKey:@"_cancelButtonText"];
<强>夫特:强>
searchBar.setValue("customString", forKey:"_cancelButtonText")
答案 1 :(得分:20)
这在ios8中对我有用,没有在ios7中尝试,但是应该这样做,小心在应用程序周期的早期放置这一行(例如:appDelegate)
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"Annuler"];
从ios9开始,你也可以使用
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"Annuler"];
Swift版本:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Annuler"
希望有所帮助;)
答案 2 :(得分:16)
找到它!在iOS 8中,取消按钮位于“cancelButton”键下。我使用UISearchBar委托searchBarTextDidBeginEditing进行更改。
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
let uiButton = searchBar.valueForKey("cancelButton") as UIButton
uiButton.setTitle("Done", forState: UIControlState.Normal)
}
答案 3 :(得分:13)
Swift 3.0:
在 AppDelegate 中添加以下内容:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "my text"
答案 4 :(得分:10)
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])。子视图包含UISearchBarBackground,UISearchBarTextField,UINavigationButto n,其中UINavigationButton是UIButton的子类。查看为UIButton的视图,如果是,则更改UIButton的属性。
答案 5 :(得分:5)
我无法在UISearchController中为UISearchBar工作。第一次显示取消按钮时,文本没有改变,但第二次是第二次。
直到我看到@ polo987的回答。这就是我所做的工作:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Done"
答案 6 :(得分:5)
在 Swift4
中更改标题:
(searchBar.value(forKey: "cancelButton") as! UIButton).setTitle("Done", for: .normal)
更改颜色:
(searchBar.value(forKey: "cancelButton") as! UIButton).setTitleColor(UIColor.blue, for: .normal)
答案 7 :(得分:4)
在斯威夫特:
searchBar.setValue("customString", forKey: "_cancelButtonText")
基于Burhanuddin Sunelwala回答。
答案 8 :(得分:4)
对于iOS 13支持:
快捷键:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Whatever"
目标C:
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:[NSArray arrayWithObject:[UISearchBar class]]] setTitle:@"Whatever"]
答案 9 :(得分: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
答案 10 :(得分:2)
我知道这似乎有点无关紧要,但在我看来,这比使用私人api更安全,而且比潜入未知视图更有效率。只有当您拥有自己的本地化引擎并且希望Apple在整个应用程序中遵循您的机制时,此解决方案才有意义。 基本上我的想法是通过改变NSUserDefaults中的“AppleLanguages”键来切换iOS SDK用于本地化按钮的语言。您可以访问here以获取有关其工作原理的更多信息。
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
无论手机上设置何种语言,都可以使用此代码来使用英语本地化和法语后备。这仅在应用程序中生效。
答案 11 :(得分:2)
迅速 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
}
}
答案 12 :(得分:0)
这是Swift解决方案:
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
}
}
}
答案 13 :(得分:-1)
let uiButton = searchBar.value(forKey: "cancelButton") as!用户界面按钮 uiButton.setTitle(NSLocalizedString("Cancel", comment: ""), for: .normal)