更改搜索栏ios的文本颜色
是否可以更改搜索栏的文本颜色?我似乎无法访问UISearchBarTextField类…首先,在更改搜索栏ios的文本颜色,ios,uisearchbar,Ios,Uisearchbar,是否可以更改搜索栏的文本颜色?我似乎无法访问UISearchBarTextField类…首先,在UISearchBar中找到子视图,然后在子视图中找到UITextField,然后更改颜色 请尝试以下代码:- for(UIView *subView in searchBar.subviews){ if([subView isKindOfClass:UITextField.class]){ [(UITextField*)subView set
UISearchBar
中找到子视图,然后在子视图中找到UITextField
,然后更改颜色
请尝试以下代码:-
for(UIView *subView in searchBar.subviews){
if([subView isKindOfClass:UITextField.class]){
[(UITextField*)subView setTextColor:[UIColor blueColor]];
}
}
对于Ios 5+
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
从iOS5开始,正确的方法是使用外观协议 例如:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
您可以这样设置属性,在控制器中调用它
[[UITextField appearanceWhenContainedIn:[self class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
*请注意,这将更改控制器中的所有UITextField原始的
UISearchBar
层次结构已更改,因为原始post和UITextField
不再是直接子视图。下面的代码不对UISearchBar
层次结构进行假设
当您不想在整个应用程序中更改搜索栏的文本颜色(即使用)时,这也很有用
用法:
[MyClass setAllTextFieldsWithin:self.mySearchBar toColor:[UIColor blueColor]];
这样做可能会起作用,但如果苹果决定使用视图层次结构,这是很容易破坏的,请参阅下面使用外观协议的我的解决方案。
[MyClass setAllTextFieldsWithin:self.mySearchBar toColor:[UIColor blueColor]];