【问题标题】:iOS Swift 3 Keep TableView content at the right position when keyboard is showniOS Swift 3在显示键盘时将TableView内容保持在正确的位置
【发布时间】:2017-03-12 09:30:10
【问题描述】:

我有一个带有 ContainerView 和 TextField 的 Viewcontroller(看起来像您的典型聊天应用程序)。 ContainerView 内部是一个嵌入的 TableView。我已经实现了一个观察者函数来增加TextField的底部布局约束,使TextField和ContainerView随着键盘向上移动。

问题是 TableView 的偏移量/插入量。看起来键盘隐藏了 TableView。

如何使 tableView 的内容与 containerView 一起移动?

这里我添加了一些截图:

这是最初的聊天视图控制器。

现在当键盘出现时,TableView 不会保持其滚动位置。看起来 containerView 只是在键盘后面,但实际上它调整了大小。

现在,当您向下滚动时,您会看到 tableView 没有保持其滚动位置。

【问题讨论】:

    标签: ios swift uitableview uiscrollview uikit


    【解决方案1】:

    试试这个:

    您可以在键盘显示时将tableViewcontentInset 设置为键盘的高度,在键盘隐藏时将其设置为0。

    您可以使用键盘通知来做到这一点:UIKeyboardWillShow、UIKeyboardWillHide

    override func viewDidLoad()
        {
            super.viewDidLoad()
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        }
    
    func keyboardWasShown(_ notification : Notification)
        {
            let info = (notification as NSNotification).userInfo
            let value = info?[UIKeyboardFrameEndUserInfoKey]
            if let rawFrame = (value as AnyObject).cgRectValue
            {
                let keyboardFrame = self.contentTableView.convert(rawFrame, from: nil)
                let keyboardHeight = keyboardFrame.height
                var contentInsets : UIEdgeInsets
                contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0)
    
                self.contentTableView.contentInset = contentInsets
                self.contentTableView.scrollIndicatorInsets = contentInsets
            }
        }
    
        func keyboardWillHide(_ notification : Notification)
        {
            let contentInsets = UIEdgeInsets.zero
            self.contentTableView.contentInset = contentInsets
            self.contentTableView.scrollIndicatorInsets = contentInsets
        }
    

    【讨论】:

    • 感谢您的帮助,但似乎插图不是问题。如果我使用您的解决方案,它只会将插图移动得很远。将offset.y 设置为+80 时,我得到了更好的结果。我希望有一个解决方案:)
    【解决方案2】:

    您可以使用观察者 UIKeyboardWillChangeFrame 并更改 tableView 的 contentOffset。

    这是如何完成的。

        NotificationCenter.default.addObserver(self
            , selector: #selector(keyboardWillChangeFrame)
            , name: NSNotification.Name.UIKeyboardWillChangeFrame
            , object: nil)
    

    这里是keyboardWillChangeFrame函数

    func keyboardWillChangeFrame(_ notification: Notification) {
        let beginFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let delta = (endFrame.origin.y - beginFrame.origin.y)
        self.threadTableView.contentOffset = CGPoint(x: 0, y: self.threadTableView.contentOffset.y - delta)
        bottomConstraint.constant = view.bounds.height - endFrame.origin.y
        self.view.layoutIfNeeded()
    }
    

    我也通过这篇文章了解了这一点。 http://derpturkey.com/maintain-uitableview-scroll-position-with-keyboard-expansion/

    【讨论】:

      【解决方案3】:

      您能否更新表格视图上的contentInset 属性以说明您已实现的观察者函数中的键盘高度?

      或者,您能否更新 ContainerView 的底部约束以考虑键盘高度?

      【讨论】:

      • 如果我更新 contentInset,我在尝试向下滚动时会在底部出现很大的间隙,并且它不会向上移动 tableview 的内容。我尝试为 containerView 底部设置约束,结果相同。
      猜你喜欢
      • 2014-08-23
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      相关资源
      最近更新 更多