点击取消按钮所在的区域会导致调用取消按钮功能,但是取消按钮本身不可见。像在输入文本之前一样轻按搜索栏会导致搜索栏升高,但是它不会调用任何委托函数,因此我无法对ui进行必要的调整。另外,点击搜索栏会使我的表视图消失,原因不明。
private func configureSearchBar() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.showsCancelButton = true
searchController.searchBar.delegate = self
definesPresentationContext = true
searchController.obscuresBackgroundDuringPresentation = false
view.addSubview(searchController.searchBar)
searchController.searchBar.frame = CGRect(x: 20, y: topBarHeight * 1.65, width: self.view.bounds.width - 40, height: 55)
let color:UIColor = .forestGreen
let lightForestGreen = color.withAlphaComponent(0.5)
searchController.searchBar.tintColor = lightForestGreen
searchController.searchBar.barTintColor = lightForestGreen
searchController.searchBar.backgroundColor = lightForestGreen
searchController.searchBar.layer.borderColor = lightForestGreen.cgColor
navigationItem.hidesSearchBarWhenScrolling = false
searchController.hidesNavigationBarDuringPresentation = true
searchController.searchBar.text = ""
searchController.searchBar.setShowsCancelButton(true, animated: false)
}
//cancel button not visible, but tapping on that area calls this function below:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("cancel button clicked...")
agendaLabel.isHidden = false
navigationController?.navigationBar.isHidden = true
tableView.reloadData()
}
//在搜索栏上点击会使其移动到屏幕顶部,但无法调用以下任何功能:
extension ScheduleViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
agendaLabel.isHidden = true
tableView.isHidden = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
agendaLabel.isHidden = true
tableView.isHidden = false
}
func searchBarResultsListButtonClicked(_ searchBar: UISearchBar) {
agendaLabel.isHidden = true
tableView.isHidden = false
}
}