iOS8无法隐藏UISearchController中搜索栏上的取消按钮 [英] iOS8 Cannot hide cancel button on search bar in UISearchController

查看:118
本文介绍了iOS8无法隐藏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:


  1. 覆盖 searchDisplayControllerDidBeginSearch 并使用以下代码:

  1. Override searchDisplayControllerDidBeginSearch and use the following code:




searchController.searchBar.showsCancelButton = false




  1. 子类UISearchBar并覆盖layoutSubviews以在系统尝试绘制var时更改该变量。

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆