2010-07-14 67 views
20

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

+1

请澄清你的问题。很难理解你想要问什么。也许发布你的代码,你有困难,或张贴一些东西的屏幕截图来帮助你解释。 – Jasarien 2010-07-14 11:01:55

+0

在Uisearchbar有文本框和搜索的图像是在那里我可以删除那个搜索图像并在那个地方添加新的图像 – a111 2010-07-14 11:48:41

回答

-4

如果你的意思是放大镜,那么没有。没有公共API来更改或删除它。改为使用UITextField

+0

好的谢谢......... – a111 2010-07-14 11:57:24

-1

其实你可以。您需要浏览搜索栏的所有子视图,然后搜索搜索字段。

0

你不能轻易删除图像,但你可以轻松地插入一个UIImageView替换图像,像这样:

UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[MBUIFactory imageNamed:@"ic_search_normal.png"]]; 
searchIcon.frame = CGRectMake(10, 10, 24, 24); 
[self.searchBar addSubview:searchIcon]; 
[searchIcon release]; 

记住 - 寿命大约为作弊;)...

的图像具有要么像素完全对齐(与像素一起玩),要么新图像必须有白色背景,需要隐藏原始图像但不影响搜索栏的其余部分(如果您只是使用白色背景整个图像,左边的角落会干扰背景)。

2

你也可以继承的UISearchBar,然后用这个方法:

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

这个隐藏放大镜。效果很好。

36

在iOS 5.0及,你可以自定义一个UISearchBar的特定实例或在使用UIAppearance代理一堆使用

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

搜索栏图像。

+1

注意:如果你试图摆脱图像完全(即不是用其他东西代替它),那么这种方法将不起作用,因为如果你将它设置为'nil',它实际上不会删除图像。 – Senseful 2013-07-29 03:51:04

+11

您还可以为一个非零图像提供属性: [_searchBar setImage:[UIImage new] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; – Diwann 2013-10-03 15:21:31

+0

这会起作用,但是推荐将文本重新对齐到左边缘的方法是什么? – 2013-11-19 00:05:49

0

在iOS6的至少有一个[searchbar setImage:<#(UIImage *)#> forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]

属性ü可以设置:)

7

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

的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) 
+1

它工作!谢谢你,杰夫。 – 2014-11-07 22:52:28

3

替换一个1x1透明图像搜索栏图标和偏移的文本位置

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

使用该代码来隐藏搜索栏的图标: -

[searchBarItems setImage:[UIImage imageNamed:@"searchIcon.jpg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 

以searchIcon.jpg名称和图标隐藏 对我来说它的工作

10

在SWIFT 2.0做到这一点:

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

如果您希望以每个控制为基础,它的工作原理也非常好 – Alnitak 2016-06-28 10:53:47

+1

为什么有人会认为可接受的答案涉及'valueForKey'?这可能会导致应用拒绝。 – TheCodingArt 2016-08-31 17:17:17

+0

http://stackoverflow.com/questions/11923597/using-valueforkey-to-access-view-in-uibarbuttonitem-private-api-violation – 2016-09-01 01:22:23

1

还是在斯威夫特4。0简单的一行:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftViewMode = .never 

注:这将从中包含内部UISearchBars ALL UITextFields取出左手图标。

相关问题