前言我使用Algolia作为searchBar小部件。我的屏幕没有导航栏,我使用取消按钮来关闭视图。如果搜索栏为启用,则此方法有效。但单击搜索按钮后,取消按钮未启用。
// MARK: - SearchBarDelegateProtocol
public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.dismiss(animated: true, completion: nil)
}
public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchViewModel.search(query: searchText)
}
public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchViewModel.search(query: searchBar.text)
self.collectionViewHeightConstraint.constant = 0
}
public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.collectionViewHeightConstraint.constant = 175
}
public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).isEnabled = true
}