I have UISearchBar and I want to remove the search marker(magnifier icon) and put a text instead of it
            Asked
            
        
        
            Active
            
        
            Viewed 1.3k times
        
    4 Answers
15
            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 :)
        mprivat
        
- 21,582
 - 4
 - 54
 - 64
 
        Abhishek Singh
        
- 6,068
 - 1
 - 23
 - 25
 
15
            
            
        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]; 
        Ali Amin
        
- 667
 - 5
 - 17
 
- 
                    Really, this should be the accepted answer. I used a variation of this line to style also the clear button. – Luke47 Dec 17 '15 at 09:58
 
2
            
            
        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:

        Andy Obusek
        
- 12,614
 - 4
 - 41
 - 62
 
1
            
            
        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.