更改UISearchBar textColor不起作用 [英] Change UISearchBar textColor not working

查看:115
本文介绍了更改UISearchBar textColor不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Google Places API用于自动完成小部件.我正在使用swift在ios 9+中显示全屏控件.我正在添加文档中给出的完整控件.

I am using google places API for autoComplete widget. I am showing the full screen control in ios 9+ using swift. I am adding a full control as given in the docs.

我添加了文档中所示的代码.现在,我想将searchBar文本颜色更改为whiteColor.

I have added the code as shown in the docs. Now I want to change the searchBar text color to whiteColor.

所以我尝试了

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

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

But I am not getting the desired behaviour. Below is the screenshot

此信息已在文档中给出

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

但是这不起作用.我需要有关此的帮助.

But this is not working. I need help with regarding to this.

推荐答案

您需要创建像follwoing这样的扩展名:

You need to create an extension like follwoing:

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
    }
}

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

And change color using following code:

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

输出为:

更新:

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

For the change color of searchBar text for the GMSAutocompleteViewController you need to do following code:

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

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

That change the text out put like following image:

如果要更改占位符文本,其颜色为searchBar.您需要执行以下代码:

And if you wish to change placeholder text and it's color for searchBar. You need to do following code:

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

它将显示为:

这篇关于更改UISearchBar textColor不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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