首页 > 代码库 > UITableView 键盘覆盖UITableViewCell的输入框解决方法(swift)
UITableView 键盘覆盖UITableViewCell的输入框解决方法(swift)
extension UITableView {
func addNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(boardWillShow(not:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(boardDidHide(not:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}
func boardWillShow(not: NSNotification) {
if let userInfo = not.userInfo {
if let keyBoardRect = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect {
UIView.animate(withDuration: 0.25) {
self.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0)
}
}
}
}
func boardDidHide(not: NSNotification) {
UIView.animate(withDuration: 0.25) {
self.contentInset = .zero
}
}
func `deinit`() {
NotificationCenter.default.removeObserver(self)
}
}
在所用的类中直接使用
self.tableView.addNotifications()
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.view.endEditing(true)
}
UITableView 键盘覆盖UITableViewCell的输入框解决方法(swift)