UISearchController searchBar 显示取消按钮不被尊重

2024-02-07

我已将 UISearchController 添加到我的应用程序中,并将其 searchBar 设置为titleView of my navigationItem.

这有效,但我看到取消按钮,尽管已经设置showsCancelButton to false.

    searchController = UISearchController(searchResultsController: searchResultsController)
    searchController.searchResultsUpdater = searchResultsUpdater


    // Configure the searchBar
    searchController.searchBar.placeholder = "Find Friends..."
    searchController.searchBar.sizeToFit()
    searchController.searchBar.showsCancelButton = false

    self.definesPresentationContext = true

    navigationItem.titleView = searchController.searchBar

我同意,这似乎是一个错误。问题是searchController不断重置showsCancelButton搜索栏的属性。我找到了一个解决方案,涉及:

  1. 子类化UISearchBar忽略setShowsCancelButton.
  2. 要使 searchController 使用该子类,您必须子类化UISearchController.
  3. 然后你发现 searchBar 没有触发搜索控制器的委托方法,所以你必须单独触发它们......

很复杂,但似乎可以解决问题。你可以找到完整的在这里回答 https://stackoverflow.com/a/28396967/3985749.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UISearchController searchBar 显示取消按钮不被尊重 的相关文章

随机推荐