更改 UISearchBar textColor 不起作用

Change UISearchBar textColor not working

我正在使用 google 个位置 API 作为自动完成小部件。我在 ios 9+ 中使用 swift 显示全屏控制。我正在添加文档中给出的完全控制。

我已经添加了文档中显示的代码。现在我想将 searchBar 文本颜色更改为 whiteColor。

所以我尝试了这个

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

但我没有得到想要的行为。下面是截图

这已在文档中给出

https://developers.google.com/places/ios-api/autocomplete#use_a_table_data_source

但这不起作用。我需要这方面的帮助。

您可以使用 UIAppearance protocol 获取 class 的外观代理,它在 iOS 5.0 及更高版本中可用。

实际上有两种方法可以自定义对象的外观和获取 class 的外观代理。

  • 要自定义 class 的所有实例的外观,请使用 appearance
  • 要自定义包含在容器 class 实例中的 class 实例或层次结构中的实例的外观,请使用 appearanceWhenContainedIn.

您可以应用此示例代码:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];

您也可以尝试此 SO post - UISearchBar text color change in iOS 7.

中给出的另一个选项

我希望能解决您的问题。编码愉快!

您需要创建如下扩展:

public extension UISearchBar {

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

并使用以下代码更改颜色:

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

输出为:

更新:

要更改 GMSAutocompleteViewController 的 searchBar 文本颜色,您需要执行以下代码:

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

它将显示为:

使用波纹管代码 Swift 3

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

希望对您有所帮助。

对于swift4ios12

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

在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

这其实很简单,只需访问那个 searchBar 中的 textField 就可以了:

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