如何在searchBar导航中隐藏canncel按钮

时间:2017-12-08 03:45:55

标签: ios swift uisearchbar

Acctualy,我在这里看过很多教程,但它仍然没有用。我真的不知道为什么。我的iOS版本是9.1。现在我真的很讨厌取消按钮。

This is my obnoxious canncel button
这是我在Viewdidload()

中的设置
override func setViewWhenDidLoad() {
        titleNoSearch.text = "Không có câu hỏi nào được tìm thấy"
        //setup search bar
        searchController.searchResultsUpdater = self
        searchController.searchBar.placeholder = "Tìm theo mô tả trong câu hỏi hoặc đáp án"

        definesPresentationContext = true
        searchController.dimsBackgroundDuringPresentation = false
        TableSearchView.tableHeaderView = searchController.searchBar
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.setShowsCancelButton(false, animated: false)
        searchController.searchBar.showsCancelButton = false
        searchController.delegate = self
        searchController.searchBar.setShowsCancelButton(false, animated: false)
        didPresentSearchController(searchController: searchController)
        // content search
        getContentSearchFullIndex()

    }

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

我尝试用另一种方法过载一些功能

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

}


class CustomSearchController: UISearchController, UISearchBarDelegate {

    lazy var _searchBar: setUISearchBar = {
        [unowned self] in
        let result = setUISearchBar(frame: CGRect.zero)
        result.delegate = self

        return result
        }()

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

0 个答案:

没有答案