在viewDidLoad()
中:
searchController = UISearchController(searchResultsController: nil)
let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)
searchController?.dimsBackgroundDuringPresentation = false
searchController?.searchResultsUpdater = self
searchController?.delegate = self
searchController?.searchBar.tintColor = UIColor.black
searchController?.searchBar.barTintColor = UIColor(white: 0.9, alpha: 0.9)
searchController?.searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchController?.hidesNavigationBarDuringPresentation = false
self.navigationItem.titleView = self.searchController?.searchBar
searchController?.isActive = true
self.searchController?.becomeFirstResponder()
self.searchController?.searchBar.becomeFirstResponder()
这是委托方法:
extension SearchMembers : UISearchControllerDelegate {
func didPresentSearchController(searchController: UISearchController){
self.searchController?.searchBar.becomeFirstResponder()
}
}
在我的测试中,didPresentSearchController(searchController:)
从未被调用。
我不知道为什么,但是我所做的一切都行不通。
有什么主意吗?
编辑 唯一可行的解决方案
delay(0.6) {
self.searchController?.searchBar.becomeFirstResponder()
}
这会造成不必要的延迟
答案 0 :(得分:0)
根据didPresentSearchController
的{{3}}:
仅当自动显示搜索控制器时才调用此方法。如果您明确显示搜索控制器,则不会调用它。
如果要显式显示搜索结果界面,请将搜索控制器包装在UISearchContainerViewController
对象中,然后显示该对象。