简体   繁体   English

在 UISearchController 中隐藏搜索栏上的取消按钮

[英]Hiding Cancel button on search bar in UISearchController

I'm trying to hide the Cancel button of the search bar in the UISearchController, but unfortunately setting the following in viewDidLoad() does not work:我试图隐藏 UISearchController 中搜索栏的取消按钮,但不幸的是,在 viewDidLoad() 中设置以下内容不起作用:

override func viewDidLoad() {
    super.viewDidLoad()

    searchResultsTableController = UITableViewController()
    searchResultsTableController.tableView.delegate = self

    searchController = UISearchController(searchResultsController: searchResultsTableController)
    searchController.searchResultsUpdater = self
    searchController.searchBar.sizeToFit()
    searchResultsView.tableHeaderView = searchController.searchBar

    searchController.delegate = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.delegate = self

    searchController.searchBar.searchBarStyle = .Minimal
    searchController.searchBar.showsCancelButton = false

    definesPresentationContext = true
}

I have also tried using the above code in this delegate method:我也试过在这个委托方法中使用上面的代码:

func didPresentSearchController(searchController: UISearchController) {
    searchController.searchBar.showsCancelButton = false
}

This approach works but will show the Cancel button briefly before hiding it, which is not ideal.这种方法有效,但会在隐藏之前短暂显示取消按钮,这并不理想。 Any suggestions?有什么建议?

I ended up subclassing both UISearchBar and UISearchController as suggested:我最终按照建议对UISearchBarUISearchController了子类化:

CustomSearchBar.swift自定义搜索栏.swift

import UIKit

class CustomSearchBar: UISearchBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        setShowsCancelButton(false, animated: false)
    }
}

CustomSearchController.swift CustomSearchController.swift

import UIKit

class CustomSearchController: UISearchController, UISearchBarDelegate {

    lazy var _searchBar: CustomSearchBar = {
        [unowned self] in
        let result = CustomSearchBar(frame: CGRectZero)
        result.delegate = self

        return result
    }()

    override var searchBar: UISearchBar {
        get {
            return _searchBar
        }
    }
}

Rejoice!麾! As of iOS 13, there is access to automaticallyShowsCancelButton on UISearchController .从 iOS 13 开始,可以访问UISearchController上的automaticallyShowsCancelButton Set it to false to hide the cancel button.将其设置为false以隐藏取消按钮。

func didPresentSearchController(_ searchController: UISearchController) {
    searchController.searchBar.becomeFirstResponder()
    searchController.searchBar.showsCancelButton = true
}


func didDismissSearchController(_ searchController: UISearchController) {
    searchController.searchBar.showsCancelButton = false
}

In my case all the above solutions dint work.就我而言,上述所有解决方案都有效。 You need to show in didPresentSearchController and hide it in didDismissSearchController.您需要在 didPresentSearchController 中显示并在 didDismissSearchController 中隐藏它。 Earlier I was just hiding in didDismissSearchController which was still showing Cancel button on cancelling.早些时候我只是躲在 didDismissSearchController 中,它仍然在取消时显示取消按钮。

Hide the Cancel button in search bar delegate methods and set your delegate searchController.searchBar.delegate=self UISearchBarDelegate隐藏搜索栏委托方法中的取消按钮并设置您的委托searchController.searchBar.delegate=self UISearchBarDelegate

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {

}

Try subclassing UISearchBar and implement:尝试UISearchBar并实现:

override func layoutSubviews() {
   super.layoutSubviews()
   self.setShowsCancelButton(false, animated: false)
}

This SO thread may help you more in this direction.这个SO 线程可能会在这个方向上为您提供更多帮助。

The same answer as given by @Griffith and @Abhinav but using extension:与@Griffith 和@Abhinav 给出的答案相同,但使用扩展名:

extension UISearchBar {
    override open func layoutSubviews() {
        super.layoutSubviews()
        setShowsCancelButton(false, animated: false)
    }
}

This code is from Swift 4.此代码来自 Swift 4。

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

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