Skip to content

Instantly share code, notes, and snippets.

@henrik
Created February 15, 2010 06:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save henrik/304457 to your computer and use it in GitHub Desktop.
Save henrik/304457 to your computer and use it in GitHub Desktop.
Replace the magnifying glass icon in an iPhone UISearchBar with a custom image.
#import <UIKit/UIKit.h>
@interface SearchBoxExperimentsViewController : UIViewController {
IBOutlet UISearchBar *searchBar;
}
@end
#import "SearchBoxExperimentsViewController.h"
@interface SearchBoxExperimentsViewController (Private)
- (void)setSearchIconToFavicon;
@end
@implementation SearchBoxExperimentsViewController
- (void)viewDidLoad {
[self setSearchIconToFavicon];
[super viewDidLoad];
}
#pragma mark Private
- (void)setSearchIconToFavicon {
// Really a UISearchBarTextField, but the header is private.
UITextField *searchField = nil;
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField) {
UIImage *image = [UIImage imageNamed: @"favicon.png"];
UIImageView *iView = [[UIImageView alloc] initWithImage:image];
searchField.leftView = iView;
[iView release];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment