How to change background color of UISearchBar in iOS7
Solution 1
Need to use:
searchBar.barTintColor = [UIColor redColor];
All thanks!
Solution 2
Set the background image to a clear image and you're good to go. This is also pre-ios 7 compatible.
searchBar.backgroundImage = [[UIImage alloc] init]
searchBar.backgroundColor = [UIColor redColor];
Solution 3
If the above solutions don't seem working then make sure that you've set the search bar style to Minimal.
[self.searchDisplayController.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
And for simple searchBar
[self.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
SearchBar Style can also be set from interface builder to Minimal.
Solution 4
it's not really work for me , or sometimes , if you too , try this
for (UIView *view in [[filterTextField.subviews objectAtIndex:0] subviews]){
if ([NSStringFromClass([view class]) isEqualToString:@"UISearchBarBackground"])
view.alpha = 0;
}
Solution 5
If the UISearchBar was defined in the MainStoryBoard, just click on that UISearchBar and take a look to the options you can handle at right. Over there if you click on the fourth tab (the one that looks like a shield) you've got a Bar Tint option. There you can select the UISearchBar color you want.
If not, I guess programatically you can do something like this:
UISearchBar* sb =[[UISearchBar alloc] init];
sb.backgroundColor=[UIColor redColor];
I hope this helps!
dev.nikolaz
Updated on July 05, 2022Comments
-
dev.nikolaz almost 2 years
How to change background color of UISearchBar in iOS7?
not gray, I want to change color like my uinavigationbar
if I Use this code, that's what comes out
searchBar.backgroundColor = [UIColor redColor];
That is not red color!!! This exact same situation as in background color of navigation bar.
-
Aladdin Gallas over 10 yearsIs that a way to remove the transparency in the searchbar ? because the color is not really red !
-
Pavan over 10 years@AladdinGallas you simply set UISearchBar's translucent property to NO like so:
[searchbar setTranslucent:NO];
-
Anish about 10 yearsi want to add rounded corners to this bar? how can i do that?
-
franklsf95 almost 10 yearsThe if clause could simply be
if ([view isKindOfClass: UISearchBarBackground)
. -
StackRunner almost 10 yearsThis is a really good answer use clearColor if you want to have a minimal design with white in the textField
-
Julian B. over 9 yearsthis does give the bar a red tone, but there is still transparency/alpha to this tone. is there a way to get rid of that and get a pure color as the tint?
-
Jeroen de Leeuw over 9 yearsSet the searchbar style to UISearchBarStyleMinimal, than you get the pure color.
-
jbouaziz over 9 years@franklsf95 It wouldn't work since it's an undocumented class.
-
Admin over 7 yearsI tried this but it didn't change the color for some reason. It only changed the color of the cursor? Anyone have any clues on why this happeneed?
-
Milan Kamilya about 7 years@1290 please check if you set
searchBarStyle = .minimal
, change it to default