Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Change UISearchBar background image?

eladoelado Posts: 3New Users
edited November 2011 in iOS SDK Development
Is it possible to change UISearchBar's background image?

I tried via a category extension class:
@implementation UISearchBar (CustomBG)
- (void)drawRect:(CGRect)rect {
	UIImage *image = [UIImage imageNamed: @"search-bg.png";];
	[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

When I put a breakpoint it does enter the function.

With UITabBar/UINavigationBar this code works perfectly.

Thanks!
Post edited by elado on
· ·

Replies

  • eladoelado Posts: 3New Users
    edited October 2009
    Answering myself:

    The UISearchBar's BG Image comes from UISearchBarBackground class, however, I have no access to it so I couldn't overwrite its drawRect.

    The solution for me is:
    UIView* segment=[searchBar.subviews objectAtIndex:0];
    	segment.hidden=YES;
    	UIView* bg=[searchBar.subviews objectAtIndex:1];
    	bg.hidden=YES;
    

    Along with the drawRect for only UISearchBar (as mentioned in the previous message).

    Works perfectly (as I don't need the segment control).
    · ·
  • jogojogo Posts: 2New Users
    edited March 2010
    thx pal! i was digging around for days to find a solution to this! :)
    · ·
  • taimur.ajmaltaimur.ajmal Posts: 4New Users
    edited June 2010
    Thanks solved my problem in minutes that would have otherwise eaten up days. :)
    · ·
  • nobre84nobre84 Posts: 981Registered Users @ @ @
    edited June 2010
    Beware that hardcoding view Indexes like this often doesn't work across different OS versions.
    · ·
  • jfmartin67jfmartin67 Posts: 80Registered Users
    edited December 2010
    elado wrote: »
    Is it possible to change UISearchBar's background image?

    I tried via a category extension class:
    @implementation UISearchBar (CustomBG)
    - (void)drawRect:(CGRect)rect {
    	UIImage *image = [UIImage imageNamed: @"search-bg.png";];
    	[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    

    When I put a breakpoint it does enter the function.

    With UITabBar/UINavigationBar this code works perfectly.

    Thanks!

    I'm using a UIViewController with a UISearchBar defined in Interface Builder and available in my code with searchBar variable. My question is, where do you put this code? I guess this is in the .m file of the UIViewController but then, it won't have access to the searchBar variable from there, Xcode reports this as an error... any advice?
    _____________________________________________<br />
    Becoming an iPhone apps developer, one line of code at a time. <img src="http://www.iphonedevsdk.com/forum/images/smilies/smile.gif" border="0" alt="" title="Smile" class="inlineimg" /><br />
    My Applic
    · ·
  • mpramodjainmpramodjain Posts: 362Registered Users
    edited February 2011
    nobre84 wrote: »
    Beware that hardcoding view Indexes like this often doesn't work across different OS versions.

    Is there any other workaround for this.
    · ·
  • daracnedaracne Posts: 3New Users
    edited November 2011
    You could try this:
    for (id aSubview in searchBar.subviews) {
       if ([aSubview isKindOfClass:NSClassFromString(@"UISearchBarBackground";)])
          [aSubview removeFromSuperview];
    }
    
    · ·
Sign In or Register to comment.