在iOS 7.x中,我们可以使用
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
setDefaultTextAttributes:@{NSForegroundColorAttributeName:myColor}];
但在iOS 9中被弃用了。我所知道的唯一解决方案是迭代 UISearchBar
的子视图层次结构,查找 UITextField
并更改其属性。但这种方法被认为是私有API使用
那么,在iOS 9.x中我们是否有合法的方法来更改 UISearchBar 文字颜色呢?
UIAppearance
协议中根本不包含这样的方法:https://gist.github.com/OgreSwamp/d554536d8f747d618483 - OgreSwamp