-3

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

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

——————

这不是问题,问题是我不能为它设置一个“边距”,以防止 UI 元素看起来过于拥挤

图片:

(1) 现在在底部输入新行并键入 / 滚动视图自动滚动到该位置 /

(2) 它应该是什么样子

在此处输入图像描述

我能做什么?

4

3 回答 3

2

我正在用 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
    }
}
于 2018-08-14T13:18:56.437 回答
0

我有一个类似的问题。只需在其周围放置一个容器(UIView)并将 UITextView 放入此容器中。您可以通过 mytextView.superview 访问这个...


这是一个如何使用他的 Container 创建 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)
        }
    }
}
于 2018-08-31T11:53:55.187 回答
-1

唯一的工作方法是在textView的底部添加一个视图..不太明白为什么

于 2018-08-09T00:16:15.000 回答