简体   繁体   English

iOS7 UITextView scrollEnabled = YES height

[英]iOS7 UITextView scrollEnabled=YES height

I am doing a test project, and came across a problem with UITextView. 我正在做一个测试项目,并遇到了UITextView的问题。

I am dynamically getting the content size of the text in the text view, and then increasing its height when needed. 我动态地在文本视图中获取文本的内容大小,然后在需要时增加其高度。 When the height reaches the threshold I have set, I will set scrollEnabled = YES to enable scrolling. 当高度达到我设置的阈值时,我将设置scrollEnabled = YES以启用滚动。 Weird thing seems to happen as shown in the following screen shots: 似乎发生了奇怪的事情,如以下屏幕截图所示:

Before going to new line and enabling scrolling: 在转到新行并启用滚动之前:

在此输入图像描述

After entering the next character, which will enable the scrolling: 输入下一个字符后,将启用滚动:

在此输入图像描述

After that, entering another character again, the text view will become normal again with scroll enabled (in fact the height remains as in the previous screen shot, I change the height according to content size, so it become the same height before enable scroll): 之后,再次输入另一个字符,文本视图将在启用滚动后再次恢复正常(实际上高度保持与上一个屏幕截图相同,我根据内容大小更改高度,因此在启用滚动之前它变为相同的高度) :

在此输入图像描述

Anyone has came across this problem and able to solve it? 任何人都遇到过这个问题并且能够解决它吗? If this is an iOS7 bug, any other suggestion for creating a message input text box? 如果这是一个iOS7错误,任何其他建议创建一个消息输入文本框? I wonder if previous iOS versions have this problem though. 我想知道以前的iOS版本是否有这个问题。

Edited: 编辑:

It seems like this problem occurs when the textview's scrollEnabled is YES and change the textview.frame.size.height, then the height will reset to the initial height (as in the height set in Interface Builder). 当textview的scrollEnabled为YES并更改textview.frame.size.height时,似乎会出现此问题,然后高度将重置为初始高度(如在Interface Builder中设置的高度)。 Wonder if this will help for this problem. 不知道这是否有助于解决这个问题。

The following shows the code used for editing the height of the text view (it is a method for the selector which will be called upon received UITextViewTextDidChangeNotification): 下面显示了用于编辑文本视图高度的代码(它是一个选择器的方法,它将在收到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;
        }
    }
}

Over a year later and scrollEnabled is still causing problems. 一年多以后,scrollEnabled仍然引发问题。 I had a similar issue where setting scrollEnabled = true (I'm using Swift) would not cause any changes. 我有一个类似的问题,设置scrollEnabled = true (我正在使用Swift)不会导致任何更改。

I solved the problem by setting autolayout constraints on all sides of the textView. 我通过在textView的所有方面设置自动布局约束来解决问题。 Then, like you detailed here, I just set textView.frame again. 然后,就像你在这里详述的那样,我只是再次设置textView.frame My guess is that this causes some internal update, which actually turns scrolling on. 我的猜测是,这会导致一些内部更新,实际上会打开滚动。 I'm also guessing that autolayout then forces the textView to stay at the right height, as opposed to the collapse that you're experiencing. 我也猜测autolayout然后强制textView保持在正确的高度,而不是你正在经历的崩溃。

The brilliant Pete Steinberger has had a lot of problems with the UITextView and implemented a lot of fixes as a result. 辉煌的Pete Steinberger在UITextView上遇到了很多问题,并因此实现了很多修复。

His article can be found here with links to his code. 他的文章可以在这里找到他的代码的链接。

For a direct link to the code, it can be found here, but I recommend reading the post. 有关代码的直接链接,可以在此处找到,但我建议阅读帖子。

I ran into a similar issue (I'm using auto-layout) and was able to solve it with the following set up: 我遇到了类似的问题(我正在使用自动布局)并且能够通过以下设置解决它:

  1. Adding top, leading, bottom, trailing margin constraints to my text view 将top,leading,bottom,trailing margin约束添加到文本视图中
  2. Adding a greater-than-or-equal-to minimum height constraint with priority 999 (in my case this was set to 50) 添加大于或等于最小高度约束的优先级为999(在我的情况下,这被设置为50)
  3. Adding a less-than-or-equal-to maximum height constraint with priority 1000 (in my case this was set to 125) 添加小于或等于最大高度约束优先级为1000(在我的情况下,这被设置为125)
  4. Adding an equal-to height constraint with priority 1000 (set to 125) and making sure it's not installed (uncheck the 'installed' option in Interface Builder or set 'active' to NO/false on the constraint in code) 添加优先级为1000(设置为125)的等高高度约束并确保未安装(在Interface Builder中取消选中'installed'选项或在代码中的约束中将'active'设置为NO / false)

I then use the following code to determine the height of the text view and enable/disable scroll and constraints: 然后,我使用以下代码来确定文本视图的高度并启用/禁用滚动和约束:

- (void)textViewDidChange:(UITextView *)textView {
    ...

    CGSize size = textView.bounds.size;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(size.width, CGFLOAT_MAX)];

    if (newSize.height >= self.textViewMaxHeightConstraint.constant
        && !textView.scrollEnabled) {
        textView.scrollEnabled = YES;
        self.textViewHeightConstraint.active = YES;
    } else if (newSize.height < self.textViewMaxHeightConstraint.constant
               && textView.scrollEnabled) {
        textView.scrollEnabled = NO;
        self.textViewHeightConstraint.active = NO;
    }

    ...
}

Using sizeThatFits: to determine the desired size of the text view, I either set scroll enabled or disabled. 使用sizeThatFits:确定文本视图的所需大小,我设置滚动启用或禁用。 If it's enabled, I set the height constraint to active to force the text view to stay at the desired height. 如果已启用,我将高度约束设置为活动以强制文本视图保持所需高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM