簡體   English   中英

在 UISearchController 中隱藏搜索欄上的取消按鈕

[英]Hiding Cancel button on search bar in UISearchController

我試圖隱藏 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
}

我也試過在這個委托方法中使用上面的代碼:

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

這種方法有效,但會在隱藏之前短暫顯示取消按鈕,這並不理想。 有什么建議?

我最終按照建議對UISearchBarUISearchController了子類化:

自定義搜索欄.swift

import UIKit

class CustomSearchBar: UISearchBar {

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

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
        }
    }
}

麾! 從 iOS 13 開始,可以訪問UISearchController上的automaticallyShowsCancelButton 將其設置為false以隱藏取消按鈕。

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


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

就我而言,上述所有解決方案都有效。 您需要在 didPresentSearchController 中顯示並在 didDismissSearchController 中隱藏它。 早些時候我只是躲在 didDismissSearchController 中,它仍然在取消時顯示取消按鈕。

隱藏搜索欄委托方法中的取消按鈕並設置您的委托searchController.searchBar.delegate=self UISearchBarDelegate

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {

}

嘗試UISearchBar並實現:

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

這個SO 線程可能會在這個方向上為您提供更多幫助。

與@Griffith 和@Abhinav 給出的答案相同,但使用擴展名:

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

此代碼來自 Swift 4。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM