Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 UITextView scrollEnabled=YES height

Tags:

ios

textview

ios7

I am doing a test project, and came across a problem with 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. Weird thing seems to happen as shown in the following screen shots:

Before going to new line and enabling scrolling:

enter image description here

After entering the next character, which will enable the scrolling:

enter image description here

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):

enter image description here

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? I wonder if previous iOS versions have this problem though.

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). 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):

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;
        }
    }
}
like image 842
L.C. Tan Avatar asked Mar 14 '14 01:03

L.C. Tan


2 Answers

Over a year later and scrollEnabled is still causing problems. I had a similar issue where setting scrollEnabled = true (I'm using Swift) would not cause any changes.

I solved the problem by setting autolayout constraints on all sides of the textView. Then, like you detailed here, I just set textView.frame again. 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.

like image 136
Cameron Sun Avatar answered Sep 19 '22 03:09

Cameron Sun


The brilliant Pete Steinberger has had a lot of problems with the UITextView and implemented a lot of fixes as a result.

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.

like image 20
Infinity James Avatar answered Sep 21 '22 03:09

Infinity James