1

I know this questions was asked(and solved) before, but this wont work for me. as a matter of fact, I had it already solved, but this issue came back out of nowhere and struck me on the head.

I am not able to set the background of my UISearchBar transparent. I was always using:

searchBar.backgroundColor = [UIColor clearColor];   
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];

and it worked nicely... but suddenly it stopped. could be since I upgraded my xcode-version but I am not sure. I spent a couple of hours already investigating this.. Is somebody out there to do this? please point me in the right direction. thanks heaps!!!

best regards T

Robin
  • 10,011
  • 5
  • 49
  • 75
Tom
  • 65
  • 3
  • 8

3 Answers3

10

Try looping through your subviews and look for the right class:

for (UIView *subview in searchBar.subviews) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [subview removeFromSuperview];
        break;
    }
}

I'm not sure but I dont think you can assume that index 0 is the background view. The example above works for me.

picknick
  • 3,897
  • 6
  • 33
  • 48
  • Although it works, I probably wouldn't recommend doing this in production. As far as I'm aware this isn't documented in the API so there is a chance at any OS update your code could stop working. I think the best way to do what you want to do would be to code your own search bar. – Nick Cartwright Feb 09 '11 at 11:22
  • thanks for your input. I hooked up a new uisearchbar and suddenly the old one is working as well.... weird! – Tom Feb 09 '11 at 12:44
0

You just need to change the searchBarStyle property to UISearchBarStyleMinimal. By default it is UISearchBarStyleProminent. You can change it in storyboard or in your code as shown below.

self.yourSearBar.searchBarStyle = UISearchBarStyleMinimal;
Aditya
  • 333
  • 3
  • 9
0

It still works for me, so maybe something else in your code messes it up. Are you sure searchBar is properly set up (connected outlet in IB, the call is called after the view is initialized, etc...)? Best way is to just print out the content of [searchBar.subviews objectAtIndex:0], it should be a UISearchBarBackground object. And btw, since Apple can change the UISearchBar view hierarchy anytime they want, you better make some check: searchBar.subviews.count, and

if ([[searchBar.subviews objectAtIndex:0] class] == NSClassFromString(@"UISearchBarBackground")) ...

before calling that.

Enzo Tran
  • 5,750
  • 6
  • 31
  • 36
  • thanks mate. I hooked up a new searchbar and suddenly the old one is working again. weird stuff, but thank you heaps! – Tom Feb 09 '11 at 12:44