How to change color of Search Bar? - iphone

I've implemented a Search Bar and I want to change color of Search Bar. How can I do that?
I've tried with:
self.mySearchBar.backgroundColor = [UIColor redColor];
but with no success.
UPDATE (Solved):
I had to remove the default background color before I defined background color with the following code.
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
... so code would be next:
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
self.mySearchBar.backgroundColor = [UIColor redColor];

Try:
self.mySearchBar.barTintColor = [UIColor redColor];

self.mySearchBar.backgroundColor = [UIColor redColor];
won't do anything,
but
self.mySearchBar.tintColor = [UIColor redColor];
will!

self.mySearchBar.backgroundVolor = [UIColor redColor];
"Volor" or "Color
self.mySearchBar.backgroundColor = [UIColor redColor];
Also if you want to tint it:
According to the documentation : https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISearchBar_Class/Reference/Reference.html, there is a tintColor property on the UISearchBar class.
In the TableSearch example: https://developer.apple.com/library/ios/#samplecode/TableSearch/ the search bar is defined and loaded in the MainView.xib. If you want to change its tintColor or style, just do it in the xib and it will be loaded into the application.
BUT THE ONLY REAL WAY to change background color is to override it, look at the answer from the following question
UISearchBar clear background color or set background image [iphone sdk]

I'm not sure if it has changed since iOS7, but it seems that the search bar in the search display controller requires an extra loop to properly remove the UISearchBarBackground view.
I created a recursive method to properly clean the search bar.
- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break; //To avoid an extra loop as there is only one UISearchBarBackground
} else {
[self removeUISearchBarBackgroundInViewHierarchy:subview];
}
}
}
You can simply send your search bar to the method and change the color afterward, a bit like the suggested answers above.
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;

I had to set UISearchBar's searchBarStyle to .default, otherwise nothing works.

searchBar.barTintColor = UIColor.redColor()

Try this code it works fine for me.
for( UIView* v in [parent subviews] ) {
if( [v isKindOfClass:[UISearchBar class]] ) {
[(UISearchBar*)v setTintColor:[UIColor blackColor]];
}

If you have issues with top and bottom lines try to remove the UISearchBarBackground:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
for (UIView *subsubview in subview.subviews) {
if ([subsubview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subsubview removeFromSuperview];
break;
}
}
}
self.searchBar.barTintColor = [UIColor redColor];
self.searchBar.backgroundColor = [UIColor redColor];

Depends what exactly are you going to change
searchBar.barTintColor
searchBar.tintColor
Other items you are able customise thought some extra code

Related

How to color a UISearchBar so that it looks seamless with the background

I'm trying to color this UISearchBar so that it looks seamless with the background (like in the second image). Is it possible to do this? I've tried setting in IB and programmatically but no luck.
thanks for any help
Here's the desired look:
UITextField *searchField = [self.searchBar valueForKey:#"_searchField"];
[searchField setBackground:[UIImage imageNamed:#"searchfield_bg"]];
[self.searchBar setTintColor:[UIColor blackColor]];
UIImage *searchimg = [UIImage imageNamed:#"search_panel.png"];
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor colorWithPatternImage:searchimg];
[self.searchBar insertSubview:bg aboveSubview:subview];
[subview removeFromSuperview];
[bg release];
break;
}
}
Im using these images, you can use your and change the color too, my requirements were to make a black searchbar so im using black color
and it will look like:
just setTint color like this..
[searchBar setBarStyle:UIBarStyleDefault];
[searchBar setTintColor:[UIColor colorWithRed:.81 green:.14 blue:.19 alpha:1]];//set alpha with your requirement
also with image use this code its working great..
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"top-bar.png"]];
[searchBar insertSubview:bg aboveSubview:subview];
[subview removeFromSuperview];
break;
}
}
And for whole search bar with Red color then simple copy paste this code mate.. here i solve this problem
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor redColor];
[searchBar insertSubview:bg aboveSubview:subview];
[subview removeFromSuperview];
break;
}
}
[searchBar setTintColor:[UIColor redColor]];

How to implement UISearchBar with no border

I am trying to add a UISearchBar without the border around it, but it's simply not working.
This is what I have:
On the XIB file, I added UISearchBar. Style = Black Transparent. Tint = Default. None of the options (bookmarks, cancel button, scope bar, etc) are selected. Background = Default. Drawing = opaque (basically the default setting of the UISearchBar).
I have a property for UISearchBar - searchBar.
In the implementation file, when the view loads, I have:
[[self.searchBar.subviews objectAtIndex:0] removeFromSuperview];
But for whatever reason, the background (the dark border around the box) is simply not going.
I tried [[self.searchBar.subviews objectAtIndex:0] setAlpha:0], but that's not working at all either.
Any help please?
This worked for me
for (UIView * view in [[[self.searchBar subviews] objectAtIndex:0] subviews])
{
if (![view isKindOfClass:[UITextField class]])
{
view .alpha = 0;
}
}
Here is the code .....
for (id img in searchBar.subviews)
{
if ([img isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[img removeFromSuperview];
}
}
happy coding :)
How about:
[self.searchBar setOpaque:NO];
[[_searchBar.subviews objectAtIndex:0] removeFromSuperview];
_searchBar.backgroundColor = [UIColor clearColor];
Hope this work for you, its working for me.
this work only in static UISearchBar, not if you want to show search CancelButton, this problem occur in devices having ios above 5.0.
For that you have to change/set BackgroundView of UiSearchBar.
Try This
for (UIView * view in [mySearchBar subviews]) {
if (![view isKindOfClass:[UITextField class]]) {
view .alpha = 0;
}
}
For those of you using 8.0 and above, the best solution is to simply use the UIAppearance protocol, by doing the following:
[[UISearchBar appearance] setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
You can also do it on one specific instance with:
[mySearchBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
if someone has same problem as I (see picture) code above could fix
[self.tableView setContentInset:UIEdgeInsetsMake(-1, 0, 0, 0)];

how to set background color as a clear color of searchbar

I want to make searchbar's background as a clear or looklike navigationbar's color
currently showing
how to make the backgroung clear of serach bar?
Beacause UI SearchBar has got its own background view, which is of black color, if we remove that view then UISearchbar's background will become clear :
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
//Search bar(using background image)
UISearchBar* bar = [[UISearchBar alloc] initWithFrame:CGRectMake(568, 30, 200, 44)];
UIImageView* iview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"blue.png"]];
iview.frame = CGRectMake(0, 0, 200, 44);
[bar insertSubview:iview atIndex:1];
[iview release];
[self.view addSubview:bar];
[bar release];
OR(using color)
//Search bar
UISearchBar* bar = [[UISearchBar alloc] initWithFrame:CGRectMake(568, 30, 200, 44)];
[[[bar subviews] objectAtIndex:0] setAlpha:0.0];
bar.tintColor = [UIColor colorWithRed:.376f green:.386f blue:.452f alpha:1.0];
bar.clipsToBounds = YES;
[self.view addSubview:bar];
[bar release];
Set the background image for the UISearchBar as follows.
[searchBar setBackgroundImage:[UIImage new]];
Now the UISearchBar background color will disappear. Will also work in iOS 7.1
For Clear the background color (If you want images set then use comment code)
for (UIView *subview in searchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
// if You want set iamge then use comment code
/* UIView *backgroundView = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"searchBar"]];
[self.searchBar insertSubview:backgroundView aboveSubview:subview];*/
[subview removeFromSuperview];
}
}
For clearing search bar's background view :
[[[self.searchBar subviews] objectAtIndex:0] setHidden:YES];
for (id subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
}
}
I found in iOS8 that this could worked better for reducing a UISearchBar to just a text field. This will allow you to get rid of the background color.
for (UIView *subView in self.searchBar.subviews) {
for (UIView *secondLevelSubview in subView.subviews) {
if (![secondLevelSubview isKindOfClass:NSClassFromString(#"UISearchBarTextField")]) {
[secondLevelSubview removeFromSuperview];
}
}
}
Try with this line :
[[ [searchBar subviews] objectAtIndex:0] removeFromSuperview];
[[ [searchBar subviews] objectAtIndex:0] setBackgroundColor:[UIColor yellowColor]];
Surely help you out.

Is it possible to remove UISearchBar's tint image?

i have to hide searchbar tint.
i write foll0wing code
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 15, 270, 15)];
searchBar.delegate = self;
searchBar.barStyle = UISegmentedControlStylePlain;
searchBar.placeholder =#"search";
searchBar.tintColor=[UIColor clearColor];
[imageview addSubview:searchBar];
here search bar add on imageview. i want to hide tint side but bydeaflt it comes .i also chage the color to tint color but it not work.is anybody have idea of removing or hide tint on searchbar .Thanks
Hope this helps.
[searchBar setBackgroundImage:[UIImage new]];
Will also work in iOS 7.1
I have used this code. It completely removed the tint around the search text bar.
for (id v in [self.search subviews]) {
if (![v isKindOfClass:NSClassFromString(#"UISearchBarTextField")]) {
[v setAlpha:0.0f];
[v setHidden:YES];
}
else {
[v setBackgroundColor:[UIColor clearColor]];
}
}
Here's how I customized the UISearchBar
if ([[[searchBar subviews] objectAtIndex:0] isKindOfClass:[UIImageView class]]){
[[[searchBar subviews] objectAtIndex:0] removeFromSuperview];
}
This removes the background bar style.
You can then add any custom subview you want.
Try this
[searchBar setTranslucent:YES];
Also, don't set the tintColor when using translucent

UIWebView underside

Is there a way to change the color of the gray underside of the UIWebView that's seen when the page is overscrolled?
Just set the backgroundColor property.
Below I mentioned swift 3 syntax, currently instead of Color, Xcode is providing color plate for mentioning color
webview.backgroundColor = Color.white
[self.webView setBackgroundColor:[UIColor clearColor]];
for(UIView* subview in [self.webView1 subviews]){
if([subview respondsToSelector:#selector(setBackgroundColor:)]){
[subview setBackgroundColor:[UIColor clearColor]];
}
for(UIView* imageView in [subview subviews]){
if([imageView isKindOfClass:[UIImageView class]]){
imageView.hidden = YES;
}
}
}