uisearchbar
让我们从UITextField
开始 (Let’s start with UITextField
)
As you probably know, Apple has made not easy for us to access the UITextField
reference on other OS versions. So here’s the code to help you guys gain safe access to UITextField
across all iOS versions:
您可能知道,Apple使我们不容易访问其他OS版本上的UITextField
参考。 因此,以下代码可帮助您在所有iOS版本中安全访问UITextField
:
更改UISearchBar左侧图标 (Change UISearchBar Left Icon)
Now, with just a single line of code, you can change the left search icon in your UISearchBar
:
现在,只需一行代码,您就可以在UISearchBar
更改左侧的搜索图标:
searchBar.setLeftImage(UIImage(named: "dark mode")!)
更改UISearchBar右侧图标 (Change UISearchBar Right Icon)
Now, with just single line of code, you can change UISearchBar
right view icon.
现在,只需一行代码,您就可以更改UISearchBar
右视图图标。
Note: This call doesn’t work in viewDidLoad
; call this above method in viewDidAppear
.
注意:此调用在viewDidLoad
不起作用; 在viewDidAppear
调用上述方法。
// Set Custom Right View
searchBar.setRightImage(normalImage: UIImage(named: “filter”)!,
highLightedImage: UIImage(named: “filter_selected”)!)
To invoke your method, on click of Right View
of UISearchBar
, override the following method of UISearchBarDelegate
:
为了调用你的方法,对点击Right View
中UISearchBar
,替换以下方法UISearchBarDelegate
:
// Override method
func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
// Filter Action
tappedFilter()
}
感谢您的阅读 (Thank you for reading)
I hope this article helps you in customizing the UISearchBar
. Please share your valuable feedback.
我希望本文能帮助您自定义UISearchBar
。 请分享您的宝贵意见。
其他文章 (Other Articles)
翻译自: https://medium.com/better-programming/how-to-change-the-search-icon-in-a-uisearchbar-150b775fb6c8
uisearchbar