[英]How to change UISearchBar Icon to custom image?
目前,我有默认的放大镜作为我的搜索栏图标。 但是,我想在它的位置放置一个自定义图像,特别是这个图像:
自定义箭头图标
我 go 如何将搜索栏默认图标更改为自定义图像?
方式1:
作为luiyezheng的回答,你可以改变UISearchBar iCon的形象。
UISearchBar.appearance().setImage(UIImage(named: "new_search_icon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
搜索图标图像更改
方式2:
你可以改变UISearchBar
iCon的搜索iCon的色调。
let textFieldInsideSearchBar = searchBarCustom.valueForKey("searchField") as? UITextField
let imageV = textFieldInsideSearchBar?.leftView as! UIImageView
imageV.image = imageV.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
imageV.tintColor = UIColor.purpleColor()
输出:
搜索图标图像色调颜色变化
你可以使用setImage
函数
searchBar.setImage(UIImage(named: "your image"), forSearchBarIcon: .Search, state: .Normal)
Swift3
searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)
lyeyezheng的回答非常好。 它的Swift 3版本是:
searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)
Swift 5 , iOS 13+
这很简单,从 iOS 开始 13 UISearchBar
有一个.searchTextField
属性,我们可能想以某种方式进行调整。
只需将图像视图设置为其.leftView
属性,并在需要时应用所需的色调。
let image = UIImage(systemName: "location")
searchBar.searchTextField.leftView = UIImageView(image: image)
searchBar.searchTextField.leftView?.tintColor = .purple
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.