Removing the image on the left of an UISearchbar
Can I remove the image on the left开发者_开发知识库 of an UISearchbar
and add a new image?
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.
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)
// 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.
In swift 2.0 do this:
let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never
Swift 4 Solution
searchBar.setImage(UIImage(), for: .search, state: .normal)
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
.
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)
How to change the position or hide magnifier icon in UISearchBar in IOS 7?
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
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);
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);
}
You can use
searchBar.setImage(UIImage(), for: .search, state: .normal)
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;
}
}
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.
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
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).
in ios6 at least there is a [searchbar setImage:<#(UIImage *)#> forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]
property u can set :)
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];
swift 5.0, xCode 12 you can do next:
searchBar.setImage(UIImage(), for: .search, state: .normal)
If you mean the magnifying glass, then no. There's no public API to change or remove that. Just use a UITextField
instead.
精彩评论