【问题标题】:how to handle tableview scroll keyboard如何处理 tableview 滚动键盘
【发布时间】:2020-08-06 03:50:18
【问题描述】:

我有一个带有带有文本字段的单元格的表格视图,最后有一个按钮,在表格视图之外,锚定到底部锚点。 我的代码似乎适用于大手机,但在 iPhone 8 上它失败了,按钮位于键盘附件视图的顶部(如果存在)。我认为是经典问题,但我不能为我的案例使用其他解决方案。

在我看来DidLoad:

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】:

    这个库有助于摆脱这个问题

    https://github.com/michaeltyson/TPKeyboardAvoiding

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-17
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多