【问题标题】:UITableView increase contentInset bottom without scrollUITableView 增加 contentInset 底部不滚动
【发布时间】:2020-07-31 02:45:41
【问题描述】:

我想在不滚动的情况下增加 tableView 内容底部插入

因为我有包含 textField 的单元格,但是当键盘出现时 textFields 是隐藏的

当键盘出现时,用户可以滚动到底部而不关闭键盘

所以我想在键盘出现时增加tableView内容底部插入而不滚动,

然后用户可以在不关闭键盘的情况下滚动到底部

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    为了获得键盘的高度,创建一个观察者,它调用一个给出键盘高度的方法,并使用这个高度计算并设置UITableView底部插图。

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    

    下面的方法是一个观察者方法,当键盘出现时会被调用。

    这里我认为UITableView 的高度与设备高度相同。如果UITableView 高度与设备高度不同,那么您必须在下面的代码中进行更改以设置正确的插入。

    @objc func keyboardWillShow(_ notification: Notification) {
        
        if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
            let keyboardRectangle = keyboardFrame.cgRectValue
            let keyboardHeight = keyboardRectangle.height
            tableview.contentInset = UIEdgeInsets(top: tableview.contentInset.top, left: tableview.contentInset.left, bottom: keyboardHeight, right: tableview.contentInset.right)
        }
    }
    

    当键盘消失时会调用下面的委托方法,所以我们将底部内容插入设置为零。

    func textFieldDidEndEditing(_ textField: UITextField) {
        tableview.contentInset = UIEdgeInsets(top: tableview.contentInset.top, left: tableview.contentInset.left, bottom: 0.0, right: tableview.contentInset.right)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2016-04-07
      相关资源
      最近更新 更多