移除UISearchbar左侧的图标

33

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


1
请澄清您的问题。很难理解您想要问什么。也许您可以发布您遇到困难的代码,或者发布一张截图来帮助您解释。 - Jasarien
在 UISearchBar 中有一个文本字段和一个搜索图标。我能否删除搜索图标并在那个位置添加新图标? - a111
19个回答

44

iOS 5.0+ 中,您可以使用

searchBar.setImage

自定义搜索栏图像。

- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state

在特定的 UISearchBar 实例中或通过使用 UIAppearance 代理在一大堆实例中应用。


1
注意:如果你想完全摆脱这个图片(即不用其他东西替换它),那么这种方法行不通,因为如果你将其设置为 nil,它实际上并不会删除该图片。 - Senseful
13
你可以将一个非空图像赋给搜索栏: [_searchBar setImage:[UIImage new] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; - Diwann
1
这个可以工作,但是推荐的方法是如何将文本重新对齐到左边缘? - Danyal Aytekin
您可以使用此方法为搜索栏图标设置1x1像素的透明图像,从而完全摆脱搜索图标。然后,您还可以设置searchTextPositionAdjustment属性来定位文本的位置。 - Vicarius
1
要重新对齐文本,您可以使用[[UISearchBar appearance] setPositionAdjustment:UIOffsetMake(-10, 0) forSearchBarIcon:UISearchBarIconSearch]; - user289841
1
searchTextPositionAdjustment 在 iOS 12 上不再需要。setPositionAdjustment 将右侧的删除按钮移动到左侧。[UISearchBar appearance] 会破坏其他搜索栏。 - Dmitry

14
为了完全移除图标,你可以使用以下代码行:

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

迅捷语言:

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

第二行在iOS 12上运行不正确。并且它会破坏其他搜索栏。 - Dmitry

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

这会隐藏放大镜图标。效果很好。


1
这并没有解决在 iOS 12 上移除图标后留下的空白问题。 - Dmitry

10

在Swift 2.0中,做如下操作:

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

这也非常好用,如果您想在每个控件上进行设置。 - Alnitak
3
为什么有人认为一个可接受的答案包含valueForKey?这可能导致应用被拒绝。 - TheCodingArt
https://dev59.com/kGfWa4cB1Zd3GeqPejBu - Ciprian Rarau
@CiprianRarau,你的意思是,你正在推广一种不可靠、脆弱的代码解决方案,绕过正确的解决方案,并容易被苹果自动代码分析拒绝...太棒了...实际上,你刚刚证实了为什么永远不应该将这样的东西作为答案推广。 - TheCodingArt
@TheCodingArt,针对这种情况,我建议您使用Rostyslav的答案。它会迭代子视图并查找第一个UITextField。但是,我想知道您知道的最干净的实现此功能的方法是什么?祝好! - Ciprian Rarau
这并没有解决在 iOS 12 上移除图标后留下的空白问题。 - Dmitry

9

Swift 4 解决方案

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

这并没有解决在 iOS 12 上移除图标后留下的空白问题。 - Dmitry

8

Swift 4解决方案:

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

您可能也想调整左侧的间隙:

searchBar.setPositionAdjustment(UIOffset(horizontal: -20, vertical: 0), for: .search)

这将把右侧的删除按钮移动到左侧。 - Dmitry

7
在 Swift 4.0 中,可以使用简单的一行代码:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftViewMode = .never

注意:这将从所有包含在UISearchBars中的UITextFields中移除左侧图标。


它在iOS 12上将空白区域留在图标的位置。 - Dmitry
同时它会破坏项目中的其他搜索栏。 - Dmitry

6

这绝对是最简单的方法。 - Alnitak
这并没有解决在iOS 12中移除图标后留下的空白问题。而且它会破坏项目中的其他搜索栏。 - Dmitry

4

将搜索栏图标替换为1x1透明图像,并偏移文本位置

UIImage *blank = [UIImage imageNamed:@"blank"];
[self.searchBar setImage:blank forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
self.searchBar.searchTextPositionAdjustment = UIOffsetMake(-19, 0);

searchTextPositionAdjustment 在 iOS 12 上不需要。顺便问一下,为什么是19而不是20? - Dmitry

4

您可以使用

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

请使用4个空格或“-”字符将代码格式化为代码。此外,请解释您的答案,而不仅仅是用一行代码回答。 - Max

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接