ScrollView中的TextView - 禁用typig上的自动滚动?

时间:2018-06-12 05:03:05

标签: swift uiscrollview uitextview

也许问题的根源在我身上,但我找不到禁用其中scrollview的{​​{1}}的自动滚动的选项。

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

----

这不是一个问题,问题是我无法设置“边距”来防止UI元素过度拥挤的外观

图片:

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

(2)应该是什么样子

enter image description here

我该怎么办?

3 个答案:

答案 0 :(得分:2)

我正在使用UITextView进行类似的操作,UITextView具有指定的初始高度,并随着输入文本不断增加高度,直到达到最大指定高度,然后开始滚动文本。让我知道这就是您要找的东西。

enter image description here 单线 enter image description here 两行 enter image description here 三行 enter image description here 四行 enter image description here 停止增加身高

我正在使用以下委托方法。以下代码中的Height约束是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
    }
}

答案 1 :(得分:0)

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


这是一个如何使用其容器创建TextView的示例...

const obj = {};
res.forEach( el => {
    if (el.resource.name === "FORM01" && ["cost.ttl","cost.use"].includes(el.name)) {
        obj["FORM01"] = { [el.name]:  el };
    }
});

只需在电视委托方法中设置一个引用(完成编辑后不要忘记清除它)

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

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

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

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

答案 2 :(得分:-1)

唯一可行的方法是在textView的底部添加一个视图。