在iOS 9+中更改UISearchBar文本颜色



在iOS 7.x中,我们能够使用

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
    setDefaultTextAttributes:@{NSForegroundColorAttributeName:myColor}];

但它在iOS 9中遭到了抨击。我知道的唯一解决方案是遍历UISearchBar的子视图层次结构,查找UITextField并更改其属性。但这种方式被视为私有API使用

我们是否有合法的方法在iOS 9.x中更改UISearchBar文本颜色?

您现在需要使用appearanceWhenContainedInInstancesOfClasses:

例如:

[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTextColor:[UIColor whiteColor]];

最新更新