Tander Tander - 4 months ago 27
Objective-C Question

Changing UISearchBar icon?

I'm trying to customise the

UISearchBar
in 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.

Would appreciate any help with this!
Thanks

Edit: The duplicated question mentioned doesn't solve the problem. Non the 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];

Answer

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