如何处理tableview滚动键盘

问题描述 投票:0回答:1

我有一个tableview,里面有带textFields的单元格,最后有一个按钮,在tableview外面,锚定在底部锚定.我的代码看起来对大手机很好,但在iPhone 8上却失败了,按钮就在键盘附件视图的顶部(如果存在的话)。我想这是一个典型的问题,但我不能使用其他的解决方案。

在我的viewDidLoad:

bindKeyboardNotifications(using: bottomContraintButtonSuperView, view: myTable)

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //dismiss keyboard observer
        NotificationCenter.default.removeObserver(self)
    }

在控制器的视图中

 ///handles the scroll of the keyboard based on a given constraint outlet
    func bindKeyboardNotifications(using constraint: NSLayoutConstraint, view: UITableView? = nil) {
        NotificationCenter.default.addObserver(forName: UIApplication.keyboardWillShowNotification, object: nil, queue: nil) { notification in
            let keyboardHeight: CGFloat = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height ?? 0
//            constraint.constant  = -20 + keyboardHeight
            constraint.constant = keyboardHeight
        }
        NotificationCenter.default.addObserver(forName: UIApplication.keyboardWillHideNotification, object: nil, queue: nil) { _ in
            constraint.constant = 20.0
        }
    }
swift uitableview keyboard
1个回答
0
投票

这个库对解决这个问题很有帮助。

https:/github.commichaeltysonTPKeyboardAvoiding

© www.soinside.com 2019 - 2024. All rights reserved.