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)

}

 

posted @ 2017-05-25 09:54  huangbiyong  阅读(769)  评论(0编辑  收藏  举报