ios - GMSAutocompleteViewController iOS,如何更改搜索栏中的文本颜色

标签 ios swift swift2 google-places

<分区>

我正在使用 GMSAutocompleteViewController 并想更改 searchBar 中的 textColor 但找不到方法,我设法更改了一些颜色但没有更改 searchBar 文本。

enter image description here

我尝试了以下代码,但颜色不会改变:

        acController.searchBarController?.searchBar.tintColor = UIColor.whiteColor()
        acController.searchBarController?.searchBar.textColor = UIColor.whiteColor()

        acController.searchBarController?.searchBar.textField.textColor = UIColor.whiteColor()
        acController.searchBarController?.searchBar.textField.tintColor = UIColor.whiteColor()
        acController.searchBarController?.searchBar.textField.backgroundColor = UIColor.whiteColor()

        acController.searchDisplayController?.searchBar.setTextColor(UIColor.whiteColor())
        acController.searchDisplayController?.searchBar.tintColor = UIColor.whiteColor()

        // changes the color of the sugested places
        acController.primaryTextColor = UIColor.whiteColor()
        acController.secondaryTextColor = UIColor.whiteColor()

最佳答案

对于那些正在寻找 Swift4 版本的人,以下代码适用于 Swift 4(Xcode 9.4 和 iOS 11)

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

对于 Swift 5

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

关于ios - GMSAutocompleteViewController iOS,如何更改搜索栏中的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39912800/

相关文章:

ios - 如何测试内部调用 NSDate() 的方法?

ios - 设备方向快速变化时如何旋转按钮?

swift - Pusher Swift 成员添加/删除事件

arrays - 为什么 [SomeStruct] 不能转换为 [Any]?

ios - 对可能尚未创建的 View 执行 segue

ios - 带 SSL 固定的 AFHTTPRequestOperation 不起作用

ios - 如何删除所有 UserDefaults 数据? - swift

swift - 通过文本域调用变量名

ios - 无法运行 bms-samples-cordova-hellopush - 未找到 bms_samples_cordova_push-Swift.h 文件

ios - UIImage 未使用 AspectFit 填充 UIImageView