隐藏取消按钮上的搜索栏在UISearchController
问题描述:
我试图隐藏取消在UISearchController搜索栏的按钮,但不幸的是设定的viewDidLoad以下()不工作:隐藏取消按钮上的搜索栏在UISearchController
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
}
这种方法工作,但会隐藏它,这是不理想的前短暂显示的取消按钮。有什么建议么?
答
我结束了两个子类和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
}
}
}
答
隐藏在搜索栏委托方法的取消按钮和设置您的委托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 thread可以帮助您更在这个方向。
您可以详细了解您的实现?你在哪里放了searchController.searchBar.showsCancelButton = false –