5

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?

OgreSwamp
  • 4,602
  • 4
  • 33
  • 54
  • Just because it's deprecated doesn't mean you can't use it (see http://stackoverflow.com/a/4133201/4062250 if you are annoyed by the warning). – deadbeef Oct 28 '15 at 12:40
  • There is no such method at all, at least in Swift `UIAppearance` protocol doesn't contain such method: https://gist.github.com/OgreSwamp/d554536d8f747d618483 – OgreSwamp Oct 28 '15 at 13:01
  • Didn't thought of that... Maybe this will help : http://stackoverflow.com/questions/24136874/appearancewhencontainedin-in-swift – deadbeef Oct 28 '15 at 13:10
  • Iterating subview hierarchy is not using private API at all. You are safe to do so. The only problem of this is the hierarchy can be changed any time. – Ryan Oct 27 '17 at 17:11

1 Answers1

1

You now need to use appearanceWhenContainedInInstancesOfClasses:

For example:

[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTextColor:[UIColor whiteColor]];
Reefwing
  • 2,242
  • 1
  • 22
  • 23