我有searchBar,当用户点击searchBar时会显示键盘,当用户点击外面时会禁用键盘。
但是,点击手势会与tableView内容进行交互。如何在键盘存在时禁用与tableView的交互?
override func viewDidLoad() {
super.viewDidLoad()
// dismiss keyboard if tapped outside of search
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true //false doesn't work
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
searchBar.resignFirstResponder()
}
答案 0 :(得分:0)
当键盘存在时添加透明UIView,并在键盘解除时将其删除。
var searchBackgroundView = UIView()
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBackgroundView = UIView(frame: CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.width, tableView.frame.height))
tableView.addSubview(searchBackgroundView)
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchBackgroundView.removeFromSuperview()
}
答案 1 :(得分:0)
两个简单的想法:
答案 2 :(得分:0)
您可以通过检查searchController
是否为active
来尝试。就像..
if searchController.active {
tableView.userInteractionEnabled = false
}
else {
tableView.userInteractionEnabled = true
}
您选择的或其他方式:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
self.tableView.userInteractionEnabled = false
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
tableView.userInteractionEnabled = true
}