Robert J. Clegg
Robert J. Clegg

Reputation: 7370

Changing UISearchBar icon?

I'm trying to customise the UISearchBarin my current app without much success. What I am trying to do is this:

enter image description here

However the end result is this:

enter image description here

You see in the second image I still have the original search icon there. I have not been able to find a way to remove it all together. I thought setting a custom background image would solve it, but evidently, not.

Edit: The suggested duplicate question mentioned doesn't solve the problem. Non the provided code works in removing the icon.

Edit 2:

This is how I have added the background image, which contains the orange icon on the left

    [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_bar"] forState:UIControlStateNormal];

Upvotes: 1

Views: 601

Answers (2)

edwardmp
edwardmp

Reputation: 6611

The following works for me on iOS 8+:

[mySearchBar setImage:[UIImage imageNamed:@"SearchBarIcon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

Upvotes: 1

Schrodingrrr
Schrodingrrr

Reputation: 4271

Even though the answer exists HERE, it does not work with iOS >= 7.x. The following will:

for (UIView *subView in self.searchedBar.subviews) {

        for (UIView *subView2 in subView.subviews) {

            if ([NSStringFromClass([subView2 class]) isEqualToString:@"UISearchBarTextField"]) {

                UITextField *searchField = (UITextField *)subView2;
                searchField.leftViewMode = UITextFieldViewModeNever;
            }
        }
    }

Upvotes: 1

Related Questions