IOS7 UITextView scrollEnabled = YES height

I am running a test project and have encountered a problem with UITextView.

I dynamically get the size of the contents of the text in the text view, and then increase it when necessary. When the height reaches the threshold that I set, I set scrollEnabled = YES to enable scrolling. A strange thing seems to be happening, as shown in the following screenshots:

Before moving to a new line and enable scrolling:

enter image description here

After entering the following character that activates scrolling:

enter image description here

After that, entering another character again, the text view will become normal again if scrolling is enabled (actually the height remains the same as in the previous screenshot, I change the height according to the size of the content, so it becomes the same height before turning on scroll):

enter image description here

Has anyone encountered this problem and were able to solve it? If this is an iOS7 bug, any other suggestion for creating a text box for entering a message? I wonder if previous iOS versions were this problem.

Edited by:

It seems like this problem occurs when textview scrollEnabled is YES and changes textview.frame.size.height, then the height will be reset to the initial height (as in the height set in Interface Builder). I wonder if this will help for this problem.

, ( , 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;
        }
    }
}
+4
3

scrollEnabled - . , scrollEnabled = true ( Swift) .

, textView. , , textView.frame. , , . , textView , , .

+3

UITextView .

.

, .

+2

( -) :

  • , , ,
  • 999 ( 50)
  • - 1000 ( 125)
  • 1000 ( 125) , ( "" "-Builder" "active" NO/false )

, / :

- (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 the scrolling enabled or disabled. If it is on, I set the height limit to active to make the text view stay at the right height.

+2
source

Source: https://habr.com/ru/post/1531683/


All Articles