32

Can I remove the image on the left of an UISearchbar and add a new image?

Mat
  • 202,337
  • 40
  • 393
  • 406
a111
  • 2,125
  • 5
  • 20
  • 21
  • 1
    Please clarify your question. It is difficult to understand what you're trying to ask. Perhaps post your code that you're having difficulty with, or post a screenshot of something to help you explain. – Jasarien Jul 14 '10 at 11:01
  • in Uisearchbar there is textfield and an image of search is there.can i remove that search image and add new image at that place – a111 Jul 14 '10 at 11:48

19 Answers19

44

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.

Nick Toumpelis
  • 2,717
  • 22
  • 38
  • 1
    Note: If you are trying to get rid of the image altogether (i.e. not replace it with something else), then this method won't work since if you set it to `nil`, it won't actually remove the image. – Senseful Jul 29 '13 at 03:51
  • 13
    You can also attribute a non nil image : [_searchBar setImage:[UIImage new] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; – Diwann Oct 03 '13 at 15:21
  • 1
    This works, but what is then the recommended way to realign the text to the left edge? – Danyal Aytekin Nov 19 '13 at 00:05
  • You can get rid of the search icon altogether by using this method to set a 1x1 pixel transparent image for the search bar icon. Then you can also set the searchTextPositionAdjustment property to position the text where you want it to be. – Vicarius Sep 11 '14 at 15:06
  • 1
    To realign the text you can use `[[UISearchBar appearance] setPositionAdjustment:UIOffsetMake(-10, 0) forSearchBarIcon:UISearchBarIconSearch];` –  Jan 21 '15 at 11:58
  • 1
    `searchTextPositionAdjustment` is not needed on iOS 12. `setPositionAdjustment` moves the Delete button on the right to the left. `[UISearchBar appearance] ` breaks other search bars. – Dmitry Dec 12 '19 at 23:09
14

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)
Jeff
  • 349
  • 2
  • 12
11
// 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.

Rostyslav
  • 137
  • 1
  • 3
  • 1
    This doesn't resolve the issue with the empty space in place of the removed icon on iOS 12. – Dmitry Dec 12 '19 at 22:52
10

In swift 2.0 do this:

let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never
Ciprian Rarau
  • 3,040
  • 1
  • 30
  • 27
  • this works very nicely too, if you want it on a per-control basis – Alnitak Jun 28 '16 at 10:53
  • 3
    Why does anyone think an acceptable answer involves `valueForKey`? This has the potential of app rejection. – TheCodingArt Aug 31 '16 at 17:17
  • http://stackoverflow.com/questions/11923597/using-valueforkey-to-access-view-in-uibarbuttonitem-private-api-violation – Ciprian Rarau Sep 01 '16 at 01:22
  • @CiprianRarau so what you're saying, is you're promoting an answer with unreliable fragile code, hacking around a correct solution, and prone to app rejection beyond Apple's automated code analysis... bravo.... In essence, you've just confirmed why you should never promote anything like this as an answer. – TheCodingArt Oct 30 '16 at 14:14
  • @TheCodingArt in this case I would suggest you use Rostyslav's answer. It iterates over the subviews and finds the first UITextField. But I'd like to know what's the cleanest way you know to achieve this functionality? Regards – Ciprian Rarau Oct 31 '16 at 08:53
  • This doesn't resolve the issue with the empty space in place of the removed icon on iOS 12. – Dmitry Dec 12 '19 at 22:52
9

Swift 4 Solution

searchBar.setImage(UIImage(), for: .search, state: .normal)
iOS Lifee
  • 2,091
  • 23
  • 32
  • This doesn't resolve the issue with the empty space in place of the removed icon on iOS 12. – Dmitry Dec 12 '19 at 22:52
7

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.

Wolfshead
  • 540
  • 6
  • 8
7

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)
mukis
  • 71
  • 1
  • 4
6

How to change the position or hide magnifier icon in UISearchBar in IOS 7?

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
Community
  • 1
  • 1
Shankar Shinde
  • 141
  • 2
  • 8
4

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);
Calvin
  • 462
  • 4
  • 6
3

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);
}
Dmitry
  • 527
  • 5
  • 13
3

You can use

searchBar.setImage(UIImage(), for: .search, state: .normal)
Max
  • 1,471
  • 15
  • 37
  • Please format code as code using 4 spaces or `-characters. Also please explain your answer rather than just answering with one line of code. – Max Jan 13 '20 at 17:17
2

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;
    }
}
Ayaz Alavi
  • 4,825
  • 8
  • 50
  • 68
Or Arbel
  • 2,965
  • 2
  • 30
  • 40
  • This doesn't resolve the issue with the empty space in place of the removed icon on iOS 12. – Dmitry Dec 12 '19 at 23:02
2

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.

Arthur Stepanov
  • 511
  • 7
  • 4
1

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

Tester
  • 49
  • 3
0

in ios6 at least there is a [searchbar setImage:<#(UIImage *)#> forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]

property u can set :)

Pedroinpeace
  • 1,419
  • 1
  • 14
  • 20
0

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];
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Kahkaz
  • 11
  • 2
  • This doesn't resolve the issue with the empty space in place of the removed icon on iOS 12. – Dmitry Dec 12 '19 at 23:05
0

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).

joshis
  • 347
  • 4
  • 9
0

swift 5.0, xCode 12 you can do next:
searchBar.setImage(UIImage(), for: .search, state: .normal)

Serhii
  • 7
  • 2
-5

If you mean the magnifying glass, then no. There's no public API to change or remove that. Just use a UITextField instead.

Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137