简体   繁体   English

可以在显示时更改搜索栏中的文本颜色吗?

[英]Possible to change the text color within a search bar while it is displayed?

I'm trying to display an initial prompt message within a search bar in one color, then as soon as the user starts to enter text remove the prompt and change the color of the text the user is entering. 我试图在搜索栏中以一种颜色显示初始提示消息,然后在用户开始输入文本后立即删除提示并更改用户输入的文本颜色。

I'm using this to set the initial color of the prompting message 我正在使用它来设置提示消息的初始颜色

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

Then when I get notified the user has started to enter text I attempt to change the color 然后,当我收到通知时,用户已开始输入文本,我尝试更改颜色

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) 
{
    appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.blackColor()
    ...

It seems the color can only be set before the search bar is displayed, not once it is already displayed. 似乎只能在显示搜索栏之前设置颜色,而不能在已经显示之后设置。 Anybody know any workarounds for this? 有人知道任何解决方法吗?

(Search bar is being created & displayed automatically via UISearchController) (正在通过UISearchController自动创建并显示搜索栏)

Use UISearchBar 's placeholder property to set the initial prompt message. 使用UISearchBar占位符属性设置初始提示消息。 UISearchBar will set its color to 70% gray by default, entirely independent from the text color for user-entered text. 默认情况下, UISearchBar会将其颜色设置为70%灰色,完全独立于用户输入文本的文本颜色。

var searchBar = UISearchBar()
// This message will be set to a gray color by default 
// UISearchBar will automatically clear it once the user starts typing
searchBar.placeholder = "Enter a message here..."
// This text color will be applied to the text that the user types
searchBar.textColor = UIColor.blackColor()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM