Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-09-2009, 04:27 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 3
elado is on a distinguished road
Question Change UISearchBar background image?

Is it possible to change UISearchBar's background image?

I tried via a category extension class:

Code:
@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!
elado is offline   Reply With Quote
Old 10-09-2009, 04:48 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 3
elado is on a distinguished road
Default

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:

Code:
	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).
elado is offline   Reply With Quote
Old 03-27-2010, 06:31 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 2
jogo is on a distinguished road
Default

thx pal! i was digging around for days to find a solution to this!
jogo is offline   Reply With Quote
Old 06-16-2010, 07:05 AM   #4 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 4
taimur.ajmal is on a distinguished road
Default

Thanks solved my problem in minutes that would have otherwise eaten up days.
taimur.ajmal is offline   Reply With Quote
Old 06-16-2010, 07:18 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

Beware that hardcoding view Indexes like this often doesn't work across different OS versions.
nobre84 is offline   Reply With Quote
Old 12-11-2010, 08:19 AM   #6 (permalink)
Registered Member
 
Join Date: Feb 2009
Location: Montreal, Canada
Posts: 80
jfmartin67 is on a distinguished road
Default

Quote:
Originally Posted by elado View Post
Is it possible to change UISearchBar's background image?

I tried via a category extension class:

Code:
@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?
__________________
_____________________________________________
Becoming an iPhone apps developer, one line of code at a time.
My Application: Ultimate Password Manager - A complete password manager integrated with a password generator

Building iPhone Apps Blog
jfmartin67 is offline   Reply With Quote
Old 02-23-2011, 02:04 AM   #7 (permalink)
Mobile Application Dev.
 
Join Date: Oct 2008
Location: Bangalore, india
Posts: 362
mpramodjain is on a distinguished road
Default

Quote:
Originally Posted by nobre84 View Post
Beware that hardcoding view Indexes like this often doesn't work across different OS versions.
Is there any other workaround for this.
mpramodjain is offline   Reply With Quote
Old 11-22-2011, 08:51 PM   #8 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 3
daracne is on a distinguished road
Default

You could try this:

Code:
for (id aSubview in searchBar.subviews) {
   if ([aSubview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
      [aSubview removeFromSuperview];
}
daracne is offline   Reply With Quote
Reply

Bookmarks

Tags
background image, uisearchbar

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 343
9 members and 334 guests
givensur, glenn_sayers, guusleijsten, ipodphone, mediaspree, mtl_tech_guy, Punkjumper, whitey99, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:35 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0