删除UISearchbar左侧的图像

a11*_*111 25 iphone uisearchbar

我可以删除左侧的图像UISearchbar并添加新图像吗?

Nic*_*lis 41

在iOS 5.0+中,您可以使用自定义搜索栏图像

- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
Run Code Online (Sandbox Code Playgroud)

在UISearchBar的特定实例中或使用UIAppearance代理的一堆实例.

  • 您还可以归属非零图像:[_ searchBar setImage:[UIImage new] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; (11认同)

Jef*_*eff 12

要完全删除图标,可以使用以下代码行:

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);
Run Code Online (Sandbox Code Playgroud)

迅速:

// 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)
Run Code Online (Sandbox Code Playgroud)


Ros*_*lav 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;
    }
Run Code Online (Sandbox Code Playgroud)

这隐藏了放大镜.效果很好.


Cip*_*rau 10

在swift 2.0中这样做:

let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never
Run Code Online (Sandbox Code Playgroud)

  • 为什么有人认为可接受的答案涉及“ valueForKey”?这可能会导致应用被拒绝。 (2认同)

iOS*_*fee 7

Swift 4解决方案

searchBar.setImage(UIImage(), for: .search, state: .normal)
Run Code Online (Sandbox Code Playgroud)


小智 6

如何在IOS 7中更改UISearchBar中的位置或隐藏放大镜图标?

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
Run Code Online (Sandbox Code Playgroud)


muk*_*kis 6

Swift 4解决方案:

searchBar.setImage(UIImage(), for: .search, state: .normal)
Run Code Online (Sandbox Code Playgroud)

您可能还需要调整左侧的间隙:

searchBar.setPositionAdjustment(UIOffset(horizontal: -20, vertical: 0), for: .search)
Run Code Online (Sandbox Code Playgroud)


Wol*_*ead 5

或者在Swift 4.0中简单的单行:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftViewMode = .never
Run Code Online (Sandbox Code Playgroud)

注意:这将从UITextFields包含在其中的所有内容中删除左手图标UISearchBars.


小智 5

您可以使用

searchBar.setImage(UIImage(), for: .search, state: .normal)
Run Code Online (Sandbox Code Playgroud)


Can*_*der -5

如果你指的是放大镜,那就不是。没有公共 API 可以更改或删除它。只需使用 aUITextField即可。