Removing the image on the left of an UISearchbar

2019-02-05 21:22发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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



回答4:

In swift 2.0 do this:

let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never


回答5:

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)


回答6:

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

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];


回答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.



回答8:

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


回答9:

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


回答10:

Swift 4 Solution

searchBar.setImage(UIImage(), for: .search, state: .normal)


回答11:

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



回答12:

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



回答13:

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

property u can set :)



回答14:

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


回答15:

Actually you can. You need to go through all subviews of the searchbar and then the searchtext field.