iOS改变searchBar搜索栏placeholder的字体颜色

如题所示,就是一个小tip,百度了很久都没答案,就在stackoverflow上找到了答案,方法有如下几种:

1.iOS5+ oc版:

[[UILabelappearanceWhenContainedIn:[UISearchBarclass],nil]setTextColor:[UIColorredColor]];

2.取出searcher里的textfield,再进行修改:

//取出textfield

UITextField*searchField=[searchBar valueForKey:@"_searchField"];

//改变searcher的textcolor

searchField.textColor=[UIColor redColor];

//改变placeholder的颜色

[searchField setValue:[UIColorblueColor]forKeyPath:@"_placeholderLabel.textColor"];

3.swift版

var  textFieldInsideSearchBar=searchBar.valueForKey("searchField")as?UITextField

textFieldInsideSearchBar?.textColor=UIColor.whiteColor()   

var  textFieldInsideSearchBarLabel=textFieldInsideSearchBar!.valueForKey("placeholderLabel")as?UILabel

textFieldInsideSearchBarLabel?.textColor=UIColor.whiteColor()

以上内容参考ios - UISearchBar change placeholder color - Stack Overflow

你可能感兴趣的:(iOS改变searchBar搜索栏placeholder的字体颜色)