Can I remove the image on the left of an UISearchbar
and add a new image?
How to change the position or hide magnifier icon in UISearchBar in IOS 7?
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
You can use
searchBar.setImage(UIImage(), for: .search, state: .normal)
I tried all answers on iOS 12 and 13 - and none worked correctly on both versions. The only correct answer is below:
searchField.leftViewMode = .never
if #available(iOS 13.0, *) {
searchTextPositionAdjustment = UIOffset(horizontal: -20, vertical: 0);
}
in ios6 at least there is a [searchbar setImage:<#(UIImage *)#> forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]
property u can set :)
Or in Swift 4.0 the simple one-liner:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftViewMode = .never
NOTE: This will remove the left hand icon from ALL UITextFields
that are contained inside UISearchBars
.
you can subclass UISearchBar and then use this method:
- (void)setTextFieldLeftView:(UIView *)view
{
UITextField *searchField = nil;
for (UIView *subview in self.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchField = (UITextField *)subview;
break;
}
}
if (searchField != nil)
{
searchField.leftView = view;
}
}