IOS Change the icon in the search bar

I am trying to customize the search bar. I figured out how to change the background and text box in the search bar, but I canโ€™t find a way to change the icon that is in the bar by default (the magnified glass icon).

Is there any way to do this?

+4
source share
2 answers

Open-source is your friend (such as cocoa is, huh):

- (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]; } } 

Since iOS 5.0:

 [self.testSearchBar setImage:[UIImage imageNamed: @"favicon.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 
+15
source
 searchBar.setImage(UIImage(named: "tempUser")!, forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal) 
+2
source

Source: https://habr.com/ru/post/1402534/


All Articles