Hiren
Hiren

Reputation: 12780

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 searchbar

how to make the backgroung clear of serach bar?

Upvotes: 1

Views: 4421

Answers (7)

Tony Rouse
Tony Rouse

Reputation: 1

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];
        }
    }
}

Upvotes: 0

Ronald Randon
Ronald Randon

Reputation: 1191

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

Upvotes: 2

Vishal Rai
Vishal Rai

Reputation: 35

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];
        }

    }

Upvotes: 0

Deepesh
Deepesh

Reputation: 8053

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];
    }
}

Upvotes: 0

nithin
nithin

Reputation: 2457

//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];

Upvotes: 2

Kuldeep
Kuldeep

Reputation: 2589

Try with this line :

[[ [searchBar subviews] objectAtIndex:0] removeFromSuperview];
[[ [searchBar subviews] objectAtIndex:0] setBackgroundColor:[UIColor yellowColor]];

Surely help you out.

Upvotes: -1

Kanan Vora
Kanan Vora

Reputation: 2122

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;
    }
} 

Upvotes: 4

Related Questions