隐藏UISearchController中搜索栏上的“取消”按钮 [英] Hiding Cancel button on search bar in UISearchController
本文介绍了隐藏UISearchController中搜索栏上的“取消”按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试隐藏UISearchController中搜索栏的取消按钮,但遗憾的是在viewDidLoad()中设置以下内容不起作用:
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:
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?
推荐答案
我最后继承了 UISearchBar
和 UISearchController
按照建议:
I ended up subclassing both UISearchBar
and UISearchController
as suggested:
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
}
}
}
这篇关于隐藏UISearchController中搜索栏上的“取消”按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文