the blog/wiki/website/homepage/internetpräsenz of Stefan Rinner

Sunday, May 11, 2014

UISearchbar - if you want to set its textcolor in a navbar use appearanceWhenContainedIn:[UINavigationBar class] http://www.rinner.st/blog… twitter

If you've been ever wondering how to set the text color of an UISearchBar you probably looked it up on stackoverflow and found tips like [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]]; which works well since iOS5.

But if you add this searchbar to your navigationbar by using self.searchDisplayController.displaysSearchBarInNavigationBar = YES; you'll find out that the text in your searchbar is black again which is quite unreadable if your background color happens to be dark blue as in my case.

So, back to the drawing board and looking at the pre iOS5 hacks which iterate over the searchbars subviews? No - just try [[UITextField appearanceWhenContainedIn:[UINavigationBar class], nil] setTextColor:[UIColor whiteColor]];

So we learn again, that the UISearchBar behaves strangely in general and even more so if placed within the navigation bar.