gpt4 book ai didi

iOS7 UITextView scrollEnabled=YES height

转载 作者:可可西里 更新时间:2023-11-01 04:29:59 32 4
gpt4 key购买 nike

我正在做一个测试项目,遇到了 UITextView 的问题。

我正在动态获取 TextView 中文本的内容大小,然后在需要时增加其高度。当高度达到我设置的阈值时,我将设置 scrollEnabled = YES 以启用滚动。奇怪的事情似乎发生了,如下面的屏幕截图所示:

在换行和启用滚动之前:

enter image description here

输入下一个字符后,将启用滚动:

enter image description here

之后,再次输入另一个字符, TextView 将再次恢复正常并启用滚动(实际上高度与之前的屏幕截图相同,我根据内容大小更改了高度,所以它变得与之前相同的高度启用滚动):

enter image description here

有人遇到过这个问题并能解决吗?如果这是一个 iOS7 错误,还有其他关于创建消息输入文本框的建议吗?我想知道以前的 iOS 版本是否有这个问题。

已编辑:

当textview的scrollEnabled为YES并更改textview.frame.size.height时,似乎会出现此问题,然后高度将重置为初始高度(如Interface Builder中设置的高度)。想知道这是否有助于解决这个问题。

下面显示了用于编辑 TextView 高度的代码(这是一个选择器的方法,将在收到 UITextViewTextDidChangeNotification 时调用):

NSInteger maxInputFieldWidth = self.inputTextField.frame.size.width;

CGSize maxSize = CGSizeMake(maxInputFieldWidth, 9999);
CGSize neededSize = [self.inputTextField sizeThatFits:maxSize];

NSInteger neededHeight = neededSize.height;

if (self.inputTextField.hasText)
{
[self.inputTextField scrollRangeToVisible:NSMakeRange([self.inputTextField.text length], 0)];

if (neededHeight <= TEXTVIEW_MAX_HEIGHT_IN_USE && neededHeight != previousHeight)
{
previousHeight = neededHeight;

CGRect inputTextFieldFrame = self.inputTextField.frame;
inputTextFieldFrame.size.height = neededHeight;
inputTextFieldFrame.origin.y = TEXTVIEW_ORIGIN_Y;
self.inputTextField.frame = inputTextFieldFrame;
}
else if (neededSize.height > TEXTVIEW_MAX_HEIGHT_IN_USE)
{
if (!self.inputTextField.scrollEnabled)
{
self.inputTextField.scrollEnabled = YES;

CGRect inputTextFieldFrame = self.inputTextField.frame;
inputTextFieldFrame.size.height = TEXTVIEW_MAX_HEIGHT_IN_USE;
inputTextFieldFrame.origin.y = TEXTVIEW_ORIGIN_Y;
self.inputTextField.frame = inputTextFieldFrame;
}
else if (neededHeight != previousHeight)
{
previousHeight = neededHeight;

CGRect inputTextFieldFrame = self.inputTextField.frame;
inputTextFieldFrame.size.height = TEXTVIEW_MAX_HEIGHT_IN_USE;
inputTextFieldFrame.origin.y = TEXTVIEW_ORIGIN_Y;
self.inputTextField.frame = inputTextFieldFrame;
}
}
}

最佳答案

一年多后,scrollEnabled 仍然会导致问题。我有一个类似的问题,设置 scrollEnabled = true(我使用的是 Swift)不会导致任何更改。

我通过在 textView 的所有边上设置自动布局约束解决了这个问题。然后,就像你在这里详述的那样,我再次设置了 textView.frame。我的猜测是这会导致一些内部更新,这实际上会打开滚动。我还猜测自动布局然后会强制 textView 保持在正确的高度,而不是您正在经历的崩溃。

关于iOS7 UITextView scrollEnabled=YES height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22394658/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com