ScrollView 中的 TextView - 在键入时禁用自动滚动?

TextView in ScrollView - disable auto scroll on typig?

也许问题的根源出在我身上,但我找不到在 scrollview 上禁用自动滚动的选项,里面有 textView

每当用户在滚动视图的可见区域底部输入一个新行并继续输入时,滚动视图就会移动

————-

这不是什么大问题,问题是我无法为其设置“边距”以防止 UI 元素看起来过于拥挤

图片:

(1) 现在在底部输入一个新行并键入 / scrollView 自动滚动到那个位置 /

(2) 应该是什么样子

我能做什么?

唯一可行的方法是在 textView 的底部添加一个视图。不太明白为什么

我正在对 UITextView 做一些类似的事情,它有一个指定的初始高度,并随着输入文本不断增加高度直到最大指定高度,然后开始滚动文本。让我知道这就是您要找的东西。

单线 两条线 三行 四行 停止增加高度

我正在使用如下委托方法。以下代码中的高度约束是 Storyboard 中的 IBOutlets。

func textViewDidChange(_ textView: UITextView) {
    let maxSize = CGSize(width: textView.frame.width, height: 90.0)
    let estimatedSize = textView.sizeThatFits(maxSize)
    if estimatedSize.height <= 90 {
        textView.isScrollEnabled = false
        textViewHeightConstraint.constant = estimatedSize.height
        textViewContainerHeightConstraint.constant = estimatedSize.height + 12.0
    } else {
        textView.isScrollEnabled = false
        textViewHeightConstraint.constant = 90.0
        textViewContainerHeightConstraint.constant = 102.0
    }
}

我遇到了类似的问题。只需在它周围放置一个容器 (UIView),然后将 UITextView 放入该容器中。您可以通过 mytextView.superview...

访问这个

下面是一个示例,说明如何使用容器创建 TextView...

// SuperView of textView
let container = UIView() 
self.scrollView.addSubview(container)

let textView = UITextView()
/*
your textView config
*/
container.addSubview(textView)

并且只需在 TVs 委托方法中设置一个引用(如果完成编辑,不要忘记清除它)

// Delegate of UITextView... 
func textViewDidBeginEditing(_ textView: UITextView) {
    activeTextView = textView
    currentIdentifier = textView.accessibilityIdentifier
}

这是观察者内部的片段 (KeyboardDidShow / Hide)

// activeTextView was defined inside textView
if activeTextView != nil { 
    // now you can access it via textview.superview?
    if let origin = activeTextView.superview?.frame.origin, let parentFrame = activeTextView.superview?.frame {
        if !aRect.contains(origin) {
            scrollView.scrollRectToVisible(parentFrame, animated:true)
        }
    }
}