3
votes

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?

8

8 Answers

5
votes

This worked for me

for (UIView * view in [[[self.searchBar subviews] objectAtIndex:0] subviews])
{
    if (![view isKindOfClass:[UITextField class]])
    {
        view .alpha = 0;
    }
}
4
votes

Here is the code .....

for (id img in searchBar.subviews)
{       
     if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
     {     
            [img removeFromSuperview];    
     }
}

happy coding :)

1
votes

How about:

[self.searchBar setOpaque:NO];
1
votes
[[_searchBar.subviews objectAtIndex:0] removeFromSuperview];
_searchBar.backgroundColor = [UIColor clearColor];

Hope this work for you, its working for me.

1
votes

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.

1
votes

Try This

 for (UIView * view in [mySearchBar subviews]) {
     if (![view isKindOfClass:[UITextField class]]) {
    view .alpha = 0;
     }
  }
1
votes

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];
0
votes

if someone has same problem as I (see picture) code above could fix

[self.tableView setContentInset:UIEdgeInsetsMake(-1, 0, 0, 0)];