Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UISearchBar text color in iOS 9+

In iOS 7.x we were able to use

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

But it got deprecated in iOS 9. The only solution I know - is to iterate through subview hierarchy of UISearchBar, look for UITextField and change its properties. But this way is considered as Private API Usage

Do we have legal way to change UISearchBar text color in iOS 9.x?

like image 530
OgreSwamp Avatar asked Nov 09 '22 02:11

OgreSwamp


1 Answers

You now need to use appearanceWhenContainedInInstancesOfClasses:

For example:

[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTextColor:[UIColor whiteColor]];
like image 61
Reefwing Avatar answered Nov 14 '22 21:11

Reefwing