Can I remove the image on the left of an UISearchbar
and add a new image?
Use this code to hide icon of search bar :-
[searchBarItems setImage:[UIImage imageNamed:@"searchIcon.jpg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
Take transparent image with searchIcon.jpg name and your icon hide its work for me
Swift 4 Solution
searchBar.setImage(UIImage(), for: .search, state: .normal)
Swift 4 solution:
searchBar.setImage(UIImage(), for: .search, state: .normal)
You probably also want to adjust the gap on the left side:
searchBar.setPositionAdjustment(UIOffset(horizontal: -20, vertical: 0), for: .search)
If you mean the magnifying glass, then no. There's no public API to change or remove that. Just use a UITextField
instead.
You cannot easily remove the image but you can easily insert a UIImageView to replace the image, like so:
UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[MBUIFactory imageNamed:@"ic_search_normal.png"]];
searchIcon.frame = CGRectMake(10, 10, 24, 24);
[self.searchBar addSubview:searchIcon];
[searchIcon release];
Remember - life is about cheating;)...
The image has to be either pixel-perfectly aligned (play with the pixels), or the new image must have white background where it's needed to hide the original image but that does not interfere with the rest of the search bar (if you just use white background for whole image, the left corners will interfere with the background).