目前,我有默認的放大鏡作爲我的搜索欄圖標。然而,我想把一個自定義圖像放在它的位置,特別是這個圖像:Custom Arrow Icon如何將UISearchBar圖標更改爲自定義圖像?
我該如何去改變搜索欄默認圖標到自定義圖像?
目前,我有默認的放大鏡作爲我的搜索欄圖標。然而,我想把一個自定義圖像放在它的位置,特別是這個圖像:Custom Arrow Icon如何將UISearchBar圖標更改爲自定義圖像?
我該如何去改變搜索欄默認圖標到自定義圖像?
可以使用setImage
功能
searchBar.setImage(UIImage(named: "your image"), forSearchBarIcon: .Search, state: .Normal)
方法1:
由於luiyezheng的回答,您可以更改的UISearchBar圖標的圖像。
UISearchBar.appearance().setImage(UIImage(named: "new_search_icon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
方式2:
你可以改變的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)