Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改搜索栏ios的文本颜色_Ios_Uisearchbar - Fatal编程技术网

更改搜索栏ios的文本颜色

更改搜索栏ios的文本颜色,ios,uisearchbar,Ios,Uisearchbar,是否可以更改搜索栏的文本颜色?我似乎无法访问UISearchBarTextField类…首先,在UISearchBar中找到子视图,然后在子视图中找到UITextField,然后更改颜色 请尝试以下代码:- for(UIView *subView in searchBar.subviews){ if([subView isKindOfClass:UITextField.class]){ [(UITextField*)subView set

是否可以更改搜索栏的文本颜色?我似乎无法访问UISearchBarTextField类…

首先,在
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]];