Reputation: 2678
I have UISearchBar and I want to remove the search marker(magnifier icon) and put a text instead of it
Upvotes: 3
Views: 13484
Reputation: 687
You can use UIAppearance method
Swift 3.0
UISearchBar.appearance().setImage(UIImage(named: "image_name"), for: .search, state: .normal)
Objective-c
[[UISearchBar appearance] setImage:[UIImage imageNamed:@"image_name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
Upvotes: 15
Reputation: 6166
You Can Change the magnify Icon in the following way , adapt it to your needs :-
#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
It worked for me :)
Upvotes: 15
Reputation: 11537
See this post Removing the image on the left of an UISearchbar. There is no public API to change or remove the magnifier and you should just use a UITextField.
Upvotes: 1
Reputation: 12842
Something a little more approved-API friendly is to use a normal UITextField instead, with the following code to add a label within the UITextField (if you want to remove the magnifying glass, what's your attraction to the search box anyway?):
UILabel *searchFieldLeftLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 32, 15)];
searchFieldLeftLabel.text = @"find:";
searchFieldLeftLabel.placeholder = @"e.g. wings, Boathouse";
searchFieldLeftLabel.font = [UIFont systemFontOfSize:14];
searchFieldLeftLabel.textColor = [UIColor grayColor];
self.searchFieldLeftLabel.leftView = locationSearchFieldLeftLabel;
[searchFieldLeftLabel release];
The result is a nice looking text box that looks like:
And when you start typing text, it replaces the placeholder, but the label remains:
Upvotes: 2