【问题标题】:Maintain visibility of focused UITextView in a UITableView when showing Keyboard显示键盘时保持 UITableView 中聚焦的 UITextView 的可见性
【发布时间】:2016-09-07 16:17:22
【问题描述】:

我正在动态构建一个表单,其中每个 UITableViewCell 都有各种类型的 UI 元素。

有一个UITableViewCell 包含一个UITextView,我希望在显示键盘时能够保持可见性。我查看了其他类似的问题,但无法找到解决方案。

我写了 Swift 版本的推荐者:Apple's Managing Keyboard

它不起作用有两个原因。 1.) 在TextViewWillBeginEditing 之前触发键盘通知。 2.) UITextView 的框架与UITableViewCell 的父视图相关,所以检查错误。

这是我当前的代码:

func adjustTableViewForKeyboard(notification: NSNotification) {
        let userInfo = notification.userInfo!

        let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)

        if notification.name == UIKeyboardWillHideNotification {
            self.tableView.contentInset = UIEdgeInsetsZero
        } else {
            self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)

            rect = self.view.frame;
            rect!.size.height -= keyboardViewEndFrame.height

        }
        self.tableView.scrollIndicatorInsets = self.tableView.contentInset

    }


func textViewDidBeginEditing(textView: UITextView) {
        self.activeTextView = textView;

        if(activeTextView != nil){
            // This check does NOT work due to the TextView's superview is the cell.
            if(!CGRectContainsPoint(rect!, (activeTextView?.frame.origin)!)){
                self.tableView.scrollRectToVisible((activeTextView?.frame)!, animated: true)
            }

        }
    }

这适用于能够滚动到所有单元格,但我还想确保UITextView 不会被键盘隐藏。

【问题讨论】:

    标签: ios swift uitableview keyboard


    【解决方案1】:

    您也许可以使用响应者链来解决这个问题。在您的通知处理程序中,尝试在您的文本视图上调用isFirstResponder();如果它返回true,那么你可以从那里调用表格视图滚动代码。

    【讨论】:

    • 你能解释一下为什么isFirstResponse() 会解决这个问题吗?在通知处理程序中,activeTextViewnil,因为键盘通知在 textview 的委托方法之前执行。此外,这并没有解决 tableview 滚动代码不起作用的问题,因为 UITextView 的超级视图是 UITableViewCell 而不是 UITableView
    • 您的目标是确保文本视图保持在屏幕上,但textViewDidBeginEditing() 为时已晚。我的想法是,如果isFirstResponder()返回true,那么你可以在处理键盘通知的方法中更早地执行动画。
    • 对于框架问题,只需使用convertRect(_:fromView:) 来确保您正在转换到/从正确的坐标系。
    • 你是炸弹。 convertRect 函数效果很好,虽然我在 UITextView 委托中对其进行了测试(这会导致一些问题)。也许我很密集,但我仍然不知道isFirstResponder() 会有什么帮助?我无法访问UITextView,除非我从textViewDidBeginEditing()获得它
    • 我发现我注册了错误的通知,UIKeyboardWillShowNotification 而不是UIKeyboardDidShowNotification。虽然我仍然想知道如何使用isFirstResponder()
    【解决方案2】:

    我正在构建的(尽管是静态单元格)表单也有类似的问题,我能够使用 scrollToRowAtIndexPath 方法来更新 tableview 的行定位,以使我的文本视图保持在屏幕上。

    func textViewDidBeginEditing(textView: UITextView) {
        if textView == notesTextView {
            tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 3), atScrollPosition: .Top, animated: true)
        }
    }
    

    它确实需要了解索引路径部分/行和在多个情况下的文本视图参考,但可能有用吗?

    scrollRectToVisible 也应该可以工作,但听起来你必须先使用 convertRect:toView 或类似方法将文本视图的框架转换为滚动视图的坐标系。

    希望这会有所帮助 - 干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-04
      • 2016-05-04
      • 2023-03-05
      • 2013-03-08
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多