简体   繁体   English

UISearchBar取消按钮无操作

[英]UISearchBar cancel button no action

My UISearchBar's cancel button doesn't seem to do anything when clicked. 单击时,我的UISearchBar's取消”按钮似乎没有任何作用。 Here is my current code: 这是我当前的代码:

var searchBar = UISearchBar()

and in my ViewDidLoad : 并在我的ViewDidLoad

searchBar.sizeToFit()
searchBar.searchBarStyle = .minimal
searchBar.placeholder = "Search City"
searchBar.showsCancelButton = true
searchBar.delegate = self
myTable.tableHeaderView = searchBar

Wondering if you can help me figure out what's wrong with my SearchBar and why the cancel button doesn't give any action. 想知道您是否可以帮助我找出SearchBar的问题以及为什么“取消”按钮不执行任何操作。

Implement SearchBar Delegate 实施SearchBar委托

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.setShowsCancelButton(false, animated: true)
    searchBar.resignFirstResponder() 
}

You can also hide and show cancel button as follow: 您还可以cancel button如下所示隐藏和显示cancel button

searchBar.setShowsCancelButton(false, animated: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM