iOS 7中搜索栏无法更改文本颜色

5

我需要更改UISearchBar文本字段中的文本颜色。我尝试了这篇文章中的两个答案:

UISearchBar text color change in iOS 7

但是文本仍然保持默认的灰色。我在viewDidLoad方法中设置了该代码。我可能遗漏了什么?有人遇到过同样的问题吗?

谢谢


搜索栏的默认灰色文本不是文本字段的文本,而是文本字段的占位符。 - rmaddy
@rmaddy 你说得对,我也设置了 [[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]]; 然后它就起作用了... 那么有可能同时改变文本框中小搜索图标的颜色吗? - AppsDev
你需要设置自己的图像。请参阅 UISearchBar 文档。 - rmaddy
如果问题得到解答,请将答案以回答的形式添加,并将其标记为被接受的答案。 - valheru
2个回答

2
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];

//This is done in Xcode 5.0 and its working btw i don't know whether it works with previous versions of Xcode or not....
// Also I wrote this method in viewDidLoad

2
// Get the instance of the UITextField of the search bar
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];

// Change search bar text color
searchField.textColor = [UIColor blueColor];

1
以这种方式访问私有实例变量超出了公共API的范围,因此不应使用。苹果不允许以这种方式访问私有iVAR,并且这可能会导致您的应用程序被拒绝在应用商店中发布。 - Widerberg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接