在 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
}
这种方法可行,但会在隐藏之前短暂显示“取消”按钮,这并不理想。有什么建议吗?
隐藏搜索栏委托方法中的“取消”按钮并设置您的委托searchController.searchBar.delegate=self
UISearchBarDelegate
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
}
尝试继承 UISearchBar
并实现:
override func layoutSubviews() {
super.layoutSubviews()
self.setShowsCancelButton(false, animated: false)
}
这篇 可能会在这方面对您有更多帮助。
我最终按照建议对 UISearchBar
和 UISearchController
进行了子类化:
CustomSearchBar.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
}
}
}
与@Griffith 和@Abhinav 给出的答案相同,但使用扩展名:
extension UISearchBar {
override open func layoutSubviews() {
super.layoutSubviews()
setShowsCancelButton(false, animated: false)
}
}
此代码来自Swift 4.
高兴!从 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 中,它仍然显示取消按钮。
我试图在 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
}
这种方法可行,但会在隐藏之前短暂显示“取消”按钮,这并不理想。有什么建议吗?
隐藏搜索栏委托方法中的“取消”按钮并设置您的委托searchController.searchBar.delegate=self
UISearchBarDelegate
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
}
尝试继承 UISearchBar
并实现:
override func layoutSubviews() {
super.layoutSubviews()
self.setShowsCancelButton(false, animated: false)
}
这篇
我最终按照建议对 UISearchBar
和 UISearchController
进行了子类化:
CustomSearchBar.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
}
}
}
与@Griffith 和@Abhinav 给出的答案相同,但使用扩展名:
extension UISearchBar {
override open func layoutSubviews() {
super.layoutSubviews()
setShowsCancelButton(false, animated: false)
}
}
此代码来自Swift 4.
高兴!从 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 中,它仍然显示取消按钮。