如何将 UISearchBar 图标更改为自定义图像?
How to change UISearchBar Icon to custom image?
目前,我有默认的放大镜作为我的搜索栏图标。但是,我想在其位置放置一个自定义图像,尤其是这张图像:
自定义箭头图标
如何将搜索栏默认图标更改为自定义图像?
你可以使用setImage
函数
searchBar.setImage(UIImage(named: "your image"), forSearchBarIcon: .Search, state: .Normal)
Swift3
searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)
方式一:
作为luiyezheng的回答,你可以改变UISearchBar iCon的形象。
UISearchBar.appearance().setImage(UIImage(named: "new_search_icon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
搜索图标图片更改
方式二:
您可以更改 UISearchBar
图标的搜索图标的色调。
let textFieldInsideSearchBar = searchBarCustom.valueForKey("searchField") as? UITextField
let imageV = textFieldInsideSearchBar?.leftView as! UIImageView
imageV.image = imageV.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
imageV.tintColor = UIColor.purpleColor()
输出:
搜索图标图像色调颜色更改
luiyezheng的回答很好。
它的 Swift 3 版本是:
searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)
目前,我有默认的放大镜作为我的搜索栏图标。但是,我想在其位置放置一个自定义图像,尤其是这张图像:
自定义箭头图标
如何将搜索栏默认图标更改为自定义图像?
你可以使用setImage
函数
searchBar.setImage(UIImage(named: "your image"), forSearchBarIcon: .Search, state: .Normal)
Swift3
searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)
方式一:
作为luiyezheng的回答,你可以改变UISearchBar iCon的形象。
UISearchBar.appearance().setImage(UIImage(named: "new_search_icon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
搜索图标图片更改
方式二:
您可以更改 UISearchBar
图标的搜索图标的色调。
let textFieldInsideSearchBar = searchBarCustom.valueForKey("searchField") as? UITextField
let imageV = textFieldInsideSearchBar?.leftView as! UIImageView
imageV.image = imageV.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
imageV.tintColor = UIColor.purpleColor()
输出:
搜索图标图像色调颜色更改
luiyezheng的回答很好。 它的 Swift 3 版本是:
searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)