GMSAutocompleteViewController iOS,如何更改searchBar中的文本颜色 [英] GMSAutocompleteViewController iOS, how to change the text color in the searchBar

查看:185
本文介绍了GMSAutocompleteViewController iOS,如何更改searchBar中的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是GMSAutocompleteViewController,想更改searchBar中的textColor,但找不到解决方法,我设法更改了一些颜色,但没有更改searchBar文本.

I'm using a GMSAutocompleteViewController and want to change the textColor in the searchBar but can't find a way to do it, I managed to change the some colors but not the searchBar text.

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

I've tried following code but the color won't change:

        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)中为我工作了

For those who are looking for Swift4 version, following code worked for me in 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]

这篇关于GMSAutocompleteViewController iOS,如何更改searchBar中的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆