SearchBar,如何更改文字颜色? [英] SearchBar, how to change text color?

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

问题描述

我的搜索栏具有默认的灰色文本,但我希望它是白色文本.我不知道如何使用swift更改范围栏文本的颜色,并且您无法从情节提要中进行更改.我找到的最接近的是

My search bar has default gray text, but I want it to be white text. I can't figure out how to use swift to change the scope bar text color, and you are unable to do it from storyboard. The closest I've found is

searchBarOutlet.setScopeBarButtonTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)

但是a,这也行不通.有什么想法吗?

but alas this won't work either. Any ideas?

推荐答案

访问UISearchbar内部的Textfield有点困难.

Its a little hard to access the Textfield inside the UISearchbar.

这是它的工作方式:

for subView in self.searchBarOutlet.subviews
    {
        for secondLevelSubview in subView.subviews
        {
            if (secondLevelSubview.isKindOfClass(UITextField))
            {
                if let searchBarTextField:UITextField = secondLevelSubview as? UITextField
                {
                    //set the color here like this:
                    searchBarTextField.textColor = UIColor.redColor()
                    break;
                }

            }
        }
    }

更短的解决方案:

var textFieldInsideSearchBar = yourSearchbar.valueForKey("searchField") as? UITextField 
textFieldInsideSearchBar?.textColor = yourcolor

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

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