Change the magnifying glass icon on a UISearchBar
If you want to just change the color of the default magnifying icon, you can set the image to use template mode and then set the image view’s tintColor.
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (id)view;
UIImageView *iconView = (id)textField.leftView;
iconView.image = [iconView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
iconView.tintColor = <##dimmedColor##>;
// other styling:
textField.font = <##font##>;
textField.textColor = <##activeColor##>;
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:<##searchBar##>.placeholder
attributes:@{NSForegroundColorAttributeName: <##dimmedColor##>}];
}
For Swift :-
UISearchBar.appearance().setImage(UIImage(named: "new_search_icon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
For apps which supports iOS 5 onwards, you can use the below method to do this,
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state;
UIControlStateNormal
and UIControlStateDisabled
are the two possible states for search bar.
For apps which uses OS version before this, this wont work. You might have to create a category on UISearchbar
and change the icon by enumerating the subviews.