【发布时间】:2010-07-14 10:42:16
【问题描述】:
我可以删除UISearchbar 左侧的图片并添加新图片吗?
【问题讨论】:
-
请澄清您的问题。很难理解你想问什么。也许发布您遇到困难的代码,或者发布一些截图来帮助您解释。
-
在 Uisearchbar 中有文本字段和搜索图像。我可以删除该搜索图像并在该位置添加新图像
标签: iphone uisearchbar
我可以删除UISearchbar 左侧的图片并添加新图片吗?
【问题讨论】:
标签: iphone uisearchbar
在 iOS 5.0+ 中,您可以使用自定义搜索栏图像
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
在 UISearchBar 的特定实例中或使用 UIAppearance 代理的一组实例中。
【讨论】:
nil,它将获胜'实际上并没有删除图像。
[[UISearchBar appearance] setPositionAdjustment:UIOffsetMake(-10, 0) forSearchBarIcon:UISearchBarIconSearch];
searchTextPositionAdjustment 在 iOS 12 上不需要。setPositionAdjustment 将右侧的“删除”按钮移至左侧。 [UISearchBar appearance] 打破了其他搜索栏。
要完全删除图标,可以使用以下代码行:
目标-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)
【讨论】:
// 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;
}
这隐藏了放大镜。效果很好。
【讨论】:
在 swift 2.0 中这样做:
let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.leftViewMode = UITextFieldViewMode.Never
【讨论】:
valueForKey?这可能会导致应用被拒绝。
Swift 4 解决方案
searchBar.setImage(UIImage(), for: .search, state: .normal)
【讨论】:
或者在 Swift 4.0 中简单的单行:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftViewMode = .never
注意:这将从包含在UISearchBars 中的所有UITextFields 中删除左侧图标。
【讨论】:
Swift 4 解决方案:
searchBar.setImage(UIImage(), for: .search, state: .normal)
您可能还想调整左侧的间隙:
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];
【讨论】:
将搜索栏图标替换为 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?
我在 iOS 12 和 13 上尝试了所有答案 - 没有一个在两个版本上都能正常工作。唯一正确答案如下:
searchField.leftViewMode = .never
if #available(iOS 13.0, *) {
searchTextPositionAdjustment = UIOffset(horizontal: -20, vertical: 0);
}
【讨论】:
你可以使用
searchBar.setImage(UIImage(), for: .search, state: .normal)
【讨论】:
你可以继承 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;
}
}
【讨论】:
在 Swift 中删除搜索图标使用这个:
searchBar.setImage(UIImage(), for: .search, state: .normal)
用自定义图像更改UIImage() 替换您的图像。
【讨论】:
使用此代码隐藏搜索栏的图标:-
[searchBarItems setImage:[UIImage imageNamed:@"searchIcon.jpg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
使用 searchIcon.jpg 名称获取透明图像并隐藏您的图标 它为我工作
【讨论】:
你不能轻易移除图片,但你可以轻松插入 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];
记住——生活就是作弊;)...
图像必须完全像素对齐(使用像素),或者新图像必须具有白色背景,在此需要隐藏原始图像但不干扰搜索栏的其余部分(如果您只需为整个图像使用白色背景,左角会干扰背景。
【讨论】:
在 ios6 中至少有一个[searchbar setImage:<#(UIImage *)#> forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]
你可以设置的属性:)
【讨论】:
对于特定的UISearchBar 实例。
目标-C:
// Get the inner search field.
UITextField *searchField = (UITextField *)[searchBar valueForKey:@"searchField"];
// Hide the left search icon.
[searchField setLeftViewMode:UITextFieldViewModeNever];
【讨论】:
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: textfield.frame.size.height))
textfield.leftView = paddingView
textfield.leftViewMode = .always
此解决方案去除了放大镜以及填充物。
【讨论】:
swift 5.0,xCode 12 你接下来可以做:searchBar.setImage(UIImage(), for: .search, state: .normal)
【讨论】:
如果你指的是放大镜,那么不是。没有公共 API 可以更改或删除它。只需改用UITextField。
【讨论】: