使用UISearchController显示搜索结果和键盘覆盖的UITableView
问题描述:
底部行/部分有其显示在UITableView的搜索结果中被分成字母部分和基于联系人姓名对应的行的UISearchController。使用UISearchController显示搜索结果和键盘覆盖的UITableView
当存在显示比显示的键盘的较大的UITableView几个联系人的搜索结果,底行和段将由键盘覆盖。
在显示过滤的搜索结果时,如何增加UITableView内容高度以便底部的联系人可以滚动到并变得对用户可见,以便它们不会被iOS键盘覆盖?
我使用夫特3.1和UISearchResultsUpdating代表与updateSearchResults方法来显示过滤的结果。
答
你需要的时候出现在键盘/消失照顾和设置的tableView的contentInset
相应。
在你TableViewController类中创建两个函数作为响应键盘事件:
func keyBoardWillShow(notification: NSNotification) {
if let keyBoardSize = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? CGRect {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyBoardSize.height, right: 0)
self.tableView.contentInset = contentInsets
}
}
func keyBoardWillHide(notification: NSNotification) {
self.tableView.contentInset = UIEdgeInsets.zero
}
和注册/注销的响应者ViewDidLoad
和deinit
:
override func viewDidLoad() {
super.viewDidLoad()
...
// register the responders
NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}