删除 UISearchbar 左侧的图像
我可以删除 UISearchbar
左侧的图像并添加新图像吗?
Can I remove the image on the left of an UISearchbar
and add a new image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我可以删除 UISearchbar
左侧的图像并添加新图像吗?
Can I remove the image on the left of an UISearchbar
and add a new image?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(19)
自定义搜索栏图像。
在 iOS 5.0+ 中,您可以在 UISearchBar 的特定实例中或使用
UIAppearance
代理在一组中In iOS 5.0+, you can customize the search bar image using
in a specific instance of a UISearchBar or across a bunch using the
UIAppearance
proxy.要完全删除图标,可以使用以下代码行:
Objective-C:
Swift:
To completely remove the icon, you can use the following lines of code:
Objective-C:
Swift:
这隐藏了放大镜。效果很好。
This hides magnifying glass. Works well.
在 swift 2.0 中这样做:
In swift 2.0 do this:
Swift 4 解决方案
Swift 4 Solution
Swift 4解决方案:
您可能还想调整左侧的间隙:
Swift 4 solution:
You probably also want to adjust the gap on the left side:
或者在 Swift 4.0 中,简单的一句:
注意:这将从
UISearchBars
中包含的所有UITextFields
中删除左侧图标。Or in Swift 4.0 the simple one-liner:
NOTE: This will remove the left hand icon from ALL
UITextFields
that are contained insideUISearchBars
.如何更改在 IOS 7 中的 UISearchBar 中定位或隐藏放大镜图标?
How to change the position or hide magnifier icon in UISearchBar in IOS 7?
您可以使用
You can use
将搜索栏图标替换为 1x1 透明图像并偏移文本位置
Replace the search bar icon with a 1x1 transparent image and offset the text position
在 Swift 中,要删除搜索图标,请使用以下命令:
要替换为自定义图像,请更改图像上的
UIImage()
。In Swift to remove search icon use this:
To replace with custom image change
UIImage()
on your image.我在 iOS 12 和 13 上尝试了所有答案 - 但没有一个在这两个版本上都能正常工作。唯一正确的答案如下:
I tried all answers on iOS 12 and 13 - and none worked correctly on both versions. The only correct answer is below:
您可以子类化 UISearchBar 然后使用此方法:
you can subclass UISearchBar and then use this method:
swift 5.0,xCode 12 你可以下一步:
searchBar.setImage(UIImage(),用于:.search,状态:.normal)
swift 5.0, xCode 12 you can do next:
searchBar.setImage(UIImage(), for: .search, state: .normal)
使用此代码隐藏搜索栏的图标:-
拍摄带有 searchIcon.jpg 名称的透明图像并隐藏您的图标
它对我有用
Use this code to hide icon of search bar :-
Take transparent image with searchIcon.jpg name and your icon hide
its work for me
您无法轻松删除图像,但可以轻松插入 UIImageView 来替换图像,如下所示:
记住 - 生活就是作弊;)...
图像必须像素完美对齐(使用像素),或者新图像必须具有白色背景,需要隐藏原始图像,但不会干扰搜索栏的其余部分(如果您仅对整个图像使用白色背景,则左角将干扰背景)。
You cannot easily remove the image but you can easily insert a UIImageView to replace the image, like so:
Remember - life is about cheating;)...
The image has to be either pixel-perfectly aligned (play with the pixels), or the new image must have white background where it's needed to hide the original image but that does not interfere with the rest of the search bar (if you just use white background for whole image, the left corners will interfere with the background).
在ios6中至少有一个
[searchbar setImage:<#(UIImage *)#>; forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]
你可以设置的属性:)
in ios6 at least there is a
[searchbar setImage:<#(UIImage *)#> forSearchBarIcon:<#(UISearchBarIcon)#> state:<#(UIControlState)#>]
property u can set :)
对于特定的
UISearchBar
实例。目标-C:
For a specific
UISearchBar
instance.Objective-C:
如果你指的是放大镜,那就不是。没有公共 API 可以更改或删除它。只需使用
UITextField
即可。If you mean the magnifying glass, then no. There's no public API to change or remove that. Just use a
UITextField
instead.