SearchBar,如何更改文字颜色?

36 *_*ign 3 uicolor ios searchbar swift

这是我的搜索栏的样子

我的搜索栏有默认的灰色文本,但我希望它是白色文本.我无法弄清楚如何使用swift来更改范围栏文本颜色,而你无法从故事板中做到这一点.我发现的最接近的是

searchBarOutlet.setScopeBarButtonTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)

但是,这也无济于事.有任何想法吗?

Dej*_*dar 11

它有点难以访问UISearchbar内的Textfield.

这是它的工作原理:

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

            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

更短的解决方案:

var textFieldInsideSearchBar = yourSearchbar.valueForKey(“searchField”) as? UITextField 
textFieldInsideSearchBar?.textColor = yourcolor
Run Code Online (Sandbox Code Playgroud)