Iphone 删除UISearchbar左侧的图像
我可以删除Iphone 删除UISearchbar左侧的图像,iphone,uisearchbar,Iphone,Uisearchbar,我可以删除ui搜索栏左侧的图像并添加新图像吗?如果您指的是放大镜,则不可以。没有公共API可以更改或删除该图像。只需使用UITextField。您无法轻松删除图像,但可以轻松插入UIImageView来替换图像,如下所示: UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[MBUIFactory imageNamed:@"ic_search_normal.png"]]; searchIcon.frame = CGRectM
ui搜索栏左侧的图像并添加新图像吗?如果您指的是放大镜,则不可以。没有公共API可以更改或删除该图像。只需使用UITextField
。您无法轻松删除图像,但可以轻松插入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];
记住——生活就是欺骗;)
图像必须完全对齐(与像素一起播放),或者新图像必须具有白色背景,其中需要隐藏原始图像,但不会干扰搜索栏的其余部分(如果仅对整个图像使用白色背景,则左角将干扰背景).您可以将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;
}
}
这里面藏着放大镜。工作正常。在iOS 5.0+中,您可以使用
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
在UISearchBar的特定实例中,或使用UIAppearance
代理跨越一组。在ios6中,至少有一个[searchbar setImage:forSearchBarIcon:state://代码>
属性u可以设置:)要完全删除图标,可以使用以下代码行:
目标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)
用1x1透明图像替换搜索栏图标,并偏移文本位置
UIImage *blank = [UIImage imageNamed:@"blank"];
[self.searchBar setImage:blank forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
self.searchBar.searchTextPositionAdjustment = UIOffsetMake(-19, 0);
使用此代码隐藏搜索栏的图标:-
[searchBarItems setImage:[UIImage imageNamed:@"searchIcon.jpg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
使用searchIcon.jpg名称拍摄透明图像,并隐藏图标
在swift 2.0中,这对我来说很有用,请执行以下操作:
let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never
或者在Swift 4.0中,简单的一行:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftViewMode = .never
注意:这将删除特定UISearchBar
实例中包含的所有UITextFields
中的左侧图标
目标-C:
// Get the inner search field.
UITextField *searchField = (UITextField *)[searchBar valueForKey:@"searchField"];
// Hide the left search icon.
[searchField setLeftViewMode:UITextFieldViewModeNever];
searchBar.setImage(UIImage(), for: .search, state: .normal)
Swift 4解决方案
searchBar.setImage(UIImage(), for: .search, state: .normal)
searchBar.setImage(UIImage(), for: .search, state: .normal)
Swift 4解决方案:
// Get the inner search field.
UITextField *searchField = (UITextField *)[searchBar valueForKey:@"searchField"];
// Hide the left search icon.
[searchField setLeftViewMode:UITextFieldViewModeNever];
searchBar.setImage(UIImage(), for: .search, state: .normal)
您可能还需要调整左侧的间隙:
searchBar.setPositionAdjustment(UIOffset(horizontal: -20, vertical: 0), for: .search)
我在iOS 12和iOS 13上尝试了所有答案,但没有一个在这两个版本上都能正常工作。唯一正确的答案如下:
searchField.leftViewMode = .never
if #available(iOS 13.0, *) {
searchTextPositionAdjustment = UIOffset(horizontal: -20, vertical: 0);
}
你可以用
此解决方案可移除放大镜和填充物。在Swift中,要移除搜索图标,请使用以下命令:
searchBar.setImage(UIImage(), for: .search, state: .normal)
要替换为自定义图像,请在图像上更改UIImage()
。swift 5.0、xCode 12,您可以执行以下操作:
searchBar.setImage(UIImage(),for:.search,state:.normal)
请澄清您的问题。很难理解你想问什么。可能会发布您遇到困难的代码,或者发布某个屏幕截图来帮助您解释。在Uisearchbar中有文本字段和搜索图像。我是否可以删除该搜索图像并在该位置添加新图像注意:如果您试图完全删除该图像(即不使用其他内容替换它),然后此方法将不起作用,因为如果将其设置为nil
,它实际上不会删除图像。您还可以为非nil图像设置属性:[\u searchBar setImage:[UIImage new]forSearchBarIcon:UISearchBaricSearch state:UIControlStateNormal];这是可行的,但是建议如何将文本重新对齐到左边缘?要重新对齐文本,可以使用[[UISearchBar外观]设置位置调整:UIOffsetMake(-10,0)forSearchBarIcon:UISearchBaricSearch]代码>searchTextPositionAdjustment
在iOS 12上不需要<代码>设置位置调整
将右侧的删除按钮向左移动<代码>[UISearchBar外观]
会打断其他搜索栏。这是迄今为止最简单的方法。这并不能解决iOS 12上删除图标时出现空白的问题。此外,它还打破了项目中的其他搜索栏。这也非常有效,如果您希望在每个控件上使用它,为什么有人认为一个可接受的答案涉及valueForKey
?这有可能导致应用程序被拒绝。@Ciprianraau那么你的意思是,你在用不可靠的脆弱代码来推销答案,在正确的解决方案周围进行黑客攻击,并且除了苹果的自动代码分析之外,很容易被应用程序拒绝。。。好极了。。。。本质上,你已经确认了为什么你永远不应该把这样的事情作为一个答案来推广。@Codingart在本例中,我建议你使用Rostyslav的答案。它迭代子视图并查找第一个UITextField。但是我想知道你所知道的实现这个功能的最干净的方法是什么?RegardsIt在iOS 12上留下空白代替图标。它还会打断项目中的其他搜索栏。这会将右侧的“删除”按钮向左移动。这不会解决iOS 12上用空白代替已删除图标的问题。这不会解决iOS 12上用空白代替已删除图标的问题12.这无法解决iOS 12上用空白代替已删除图标的问题。这无法解决用空白代替已删除图标的问题。这不是删除图标,而是替换图标。这不是删除图标,而是替换图标。这无法解决用空白代替已删除图标的问题在iOS 12上。第二行在iOS 12上工作不正确。它还会打断其他搜索栏。searchTextPositionAdjustment
在iOS 12上不需要。顺便问一下,为什么是19而不是20?请使用4个空格或“--字符将代码格式化为代码。另外,请解释你的答案,而不是仅仅用一行代码来回答。你自己的回答听起来很奇怪:“这应该是公认的答案:)你的评论帮助了我。泰
searchBar.setImage(UIImage(), for: .search, state: .normal)