iOS UISearchBar - 始终保持取消按钮处于活动状态

时间:2017-12-15 23:55:15

标签: ios full-text-search uisearchbar uisearchcontroller algolia

前言我使用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
}

0 个答案:

没有答案