Reputation: 2165
Can I remove the image on the left of an UISearchbar
and add a new image?
Upvotes: 34
Views: 30272
Reputation: 1
searchBar.searchTextField.leftView = nil
This will remove the magnifying glass icon from UISearchBar
. Also it won't leave blank space like the other solutions.
Upvotes: 0
Reputation: 27
swift 5.0, xCode 12 you can do next:
searchBar.setImage(UIImage(), for: .search, state: .normal)
Upvotes: 2
Reputation: 579
In Swift to remove search icon use this:
searchBar.setImage(UIImage(), for: .search, state: .normal)
To replace with custom image change UIImage()
on your image.
Upvotes: 4
Reputation: 51
You can use
searchBar.setImage(UIImage(), for: .search, state: .normal)
Upvotes: 5
Reputation: 607
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);
}
Upvotes: 3
Reputation: 81
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)
Upvotes: 8
Reputation: 2201
Swift 4 Solution
searchBar.setImage(UIImage(), for: .search, state: .normal)
Upvotes: 9
Reputation: 11
For a specific UISearchBar
instance.
Objective-C:
// Get the inner search field.
UITextField *searchField = (UITextField *)[searchBar valueForKey:@"searchField"];
// Hide the left search icon.
[searchField setLeftViewMode:UITextFieldViewModeNever];
Upvotes: 0
Reputation: 550
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
.
Upvotes: 7
Reputation: 359
To completely remove the icon, you can use the following lines of code:
Objective-C:
// Remove the icon, which is located in the left view
[UITextField appearanceWhenContainedIn:[UISearchBar class], nil].leftView = nil;
// Give some left padding between the edge of the search bar and the text the user enters
[UISearchBar appearance].searchTextPositionAdjustment = UIOffsetMake(10, 0);
Swift:
// Remove the icon, which is located in the left view
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).leftView = nil
// Give some left padding between the edge of the search bar and the text the user enters
UISearchBar.appearance().searchTextPositionAdjustment = UIOffsetMake(10, 0)
Upvotes: 15
Reputation: 3120
In swift 2.0 do this:
let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never
Upvotes: 10
Reputation: 49
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
Upvotes: 1
Reputation: 141
How to change the position or hide magnifier icon in UISearchBar in IOS 7?
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
Upvotes: 6
Reputation: 462
Replace the search bar icon with a 1x1 transparent image and offset the text position
UIImage *blank = [UIImage imageNamed:@"blank"];
[self.searchBar setImage:blank forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
self.searchBar.searchTextPositionAdjustment = UIOffsetMake(-19, 0);
Upvotes: 4
Reputation: 1439
in ios6 at least there is a [searchbar setImage:<#(UIImage *)#> forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]
property u can set :)
Upvotes: 0
Reputation: 2714
In iOS 5.0+, you can customize the search bar image using
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
in a specific instance of a UISearchBar or across a bunch using the UIAppearance
proxy.
Upvotes: 44
Reputation: 137
// hide magnifying glass
UITextField* searchField = nil;
for (UIView* subview in searchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField*)subview;
break;
}
}
if (searchField) {
searchField.leftViewMode = UITextFieldViewModeNever;
}
This hides magnifying glass. Works well.
Upvotes: 11
Reputation: 2975
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;
}
}
Upvotes: 2
Reputation: 347
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).
Upvotes: 0
Reputation: 113380
If you mean the magnifying glass, then no. There's no public API to change or remove that. Just use a UITextField
instead.
Upvotes: -5