UISearchbar barTintColor Cancel button does not appear

I am trying to set the color of the UISearchbar to one color, and tintColor (cursor and cancel button) to a different color with a code like this:

searchbar.barTintColor = color1; searchbar.tintColor = color2; 

The TintColor bar is applied correctly.

The problem is that the cancel button text is not displayed, but if you try to do the same in the storyboard (apply batTintColor and tintColor to the UISearchbar), everything works as expected.

+7
ios7 uisearchbar tintcolor
source share
1 answer

I noticed that the cancel button just does not appear when it is β€œnormal”, so I found this code that solves the problem, but I don't think this is the intended approach.

  [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateNormal]; 
+6
source share

All Articles