iOS8无法隐藏UISearchController中搜索栏上的取消按钮 [英] iOS8 Cannot hide cancel button on search bar in UISearchController
问题描述
我的目标是阻止取消按钮出现在UISearchController的搜索栏中。我开始使用 Apple的桌面搜索与UISearchController示例代码并隐藏取消按钮,如下面的代码片段所示。但是,当用户点击文本字段时,仍会显示取消按钮。有什么帮助吗?
My goal is to prevent the cancel button from appearing in a search bar in a UISearchController. I started with Apple's Table Search with UISearchController sample code and hid the cancel button as seen in the code snip below. However, when the user taps in the text field, the cancel button still appears. Any help?
override func viewDidLoad() {
super.viewDidLoad()
resultsTableController = ResultsTableController()
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.delegate = self
//Hide cancel button - added by me
searchController.searchBar.showsCancelButton = false
...
推荐答案
我认为有三个实现这一目标的方法:
I think there are three ways of achieving that:
- 覆盖 searchDisplayControllerDidBeginSearch 并使用以下代码:
- Override searchDisplayControllerDidBeginSearch and use the following code:
searchController.searchBar.showsCancelButton = false
-
子类UISearchBar并覆盖layoutSubviews以在系统尝试绘制var时更改该变量。
Subclass UISearchBar and override the layoutSubviews to change that var when the system attempts to draw it.
注册键盘通知 UIKeyboardWillShowNotification 并应用 1中的代码。
当然可以随时实现您的搜索栏。
Of course can always implement your search bar.
这篇关于iOS8无法隐藏UISearchController中搜索栏上的取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!