【问题标题】:Unable to change text color in a search bar for iOS 7无法更改 iOS 7 搜索栏中的文本颜色
【发布时间】:2013-11-03 21:01:27
【问题描述】:

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

UISearchBar text color change in iOS 7

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

谢谢

【问题讨论】:

  • 搜索栏默认的灰色文本不是文本域的文本,而是文本域的占位符。
  • @rmaddy 你说得对,我也设置了[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];,它起作用了...是否也可以更改文本字段中小搜索图标的颜色?
  • 您必须设置自己的图像。请参阅UISearchBar 文档。
  • 如果问题有答案,请将答案以答案的形式添加,并标记为已接受答案。

标签: ios ios7 uitextfield uisearchbar textcolor


【解决方案1】:
[[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];
    

    【讨论】:

    • 以这种方式访问​​私有实例变量超出了公共 API 的范围,因此不应使用。 Apple 不允许以这种方式访问​​私有 iVARS,这也可能导致您的应用被应用商店拒绝。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多