更改UISearchBar文字颜色无效。

5

我正在使用谷歌地点API来实现自动完成小部件。我正在使用Swift在iOS 9+中显示全屏控件。我按照文档中给出的方式添加了一个完整的控制器。

我已经按照文档中的示例添加了代码。现在,我想将搜索栏文本颜色更改为白色。

所以我尝试了这个:

UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()

但我没有得到期望的行为。以下是截图。 **输入图像描述** 这在文档中已经给出。 https://developers.google.com/places/ios-api/autocomplete#use_a_table_data_source 但这并没有起作用。我需要关于此事的帮助。

请解释所需的输出和您遇到的错误。 - Shubhank
最好详细阐述问题,并在可能的情况下附上截图,否则很难得到确切的解决方案。 - Shobhakar Tiwari
7个回答

10

您需要创建一个类似下面的扩展:

public extension UISearchBar {

    public func setNewcolor(color: UIColor) {
        let clrChange = subviews.flatMap { $0.subviews }
        guard let sc = (clrChange.filter { $0 is UITextField }).first as? UITextField else { return }
        sc.textColor = color
    }
}

使用以下代码更改颜色:

 controller.searchBar.setNewcolor(UIColor.redColor())

输出结果是:

输入图片描述

更新:

要更改 GMSAutocompleteViewController 搜索栏文字的颜色,您需要执行以下代码:

let searchBarTextAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes

更改文本输出如下图所示:

输入图像说明

如果您希望更改searchBar的占位符文本和颜色,则需要执行以下代码:

let placeholderAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]

let attributedPlaceholder: NSAttributedString = NSAttributedString(string: "Find a place", attributes: placeholderAttributes)
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = attributedPlaceholder

它将显示如下:

在此输入图片描述


6

使用以下代码适用于Swift 3

if #available(iOS 9.0, *) {
  UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.green]
  } else {
  // Fallback on earlier versions
}

希望这可以帮助到您。

3

针对Swift 4和iOS 12

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes
            .updateValue(UIColor.white, forKey: NSAttributedStringKey.foregroundColor.rawValue)

2
您可以使用UIAppearance协议来获取iOS 5.0及更高版本中可用的类的外观代理。
实际上,有两种方法可以自定义对象的外观并获取类的外观代理。
  • 要自定义类的所有实例的外观,请使用appearance
  • 要自定义包含在容器类的实例或层次结构中的类的实例的外观,请使用appearanceWhenContainedIn
您可以应用此示例代码:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];

您还可以尝试 SO 帖子中提供的另一种选项 - iOS 7 中 UISearchBar 文字颜色更改
希望这能解决您的问题。祝编码愉快!

它没有涵盖到我的问题。我正在使用Swift,在Swift中我们无法使用defaultText属性。 - Ranjit

1
在Swift 5中:
let searchBarTextAttributes: [NSAttributedString.Key : AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.red, NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): UIFont.systemFont(ofSize: 14.0)]

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes

1

这其实很简单,只需要像这样访问搜索栏内部的textField:

searchBar.searchTextField.defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

0

Swift 5 最新资讯

searchBar.searchTextField.textColor = .white

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接