uisearchbar_如何在uisearchbar中更改搜索图标

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

Image for post
Code snippet for accessing textfield and changing it’s properties
用于访问文本字段并更改其属性的代码段

更改UISearchBar左侧图标 (Change UISearchBar Left Icon)

Image for post
Code snippet for adding left icon in UISearchBar
用于在UISearchBar中添加左侧图标的代码段

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")!)
Image for post

更改UISearchBar右侧图标 (Change UISearchBar Right Icon)

Image for post
Code snippet for adding right icon in UISearchBar
用于在UISearchBar中添加右图标的代码段

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”)!)
Image for post

To invoke your method, on click of Right View of UISearchBar, override the following method of UISearchBarDelegate:

为了调用你的方法,对点击Right ViewUISearchBar ,替换以下方法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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值