Sunil Zalavadiya
Sunil Zalavadiya

Reputation: 1993

Issue with UITextView cursor/scroll movement

I have used UITextView for chat like screenshot attached here. enter image description here

But the problem is that If I press on return key of Keyboard, the cursor does not stick to bottom of UITextView bounds. Also I have used following code:

- (void)textViewDidChange:(UITextView *)textView
{
    if([textView contentSize].height <80.0)
    {
        CGFloat textHeight         = [self textHeightForTextView:textView];

        CGFloat newViewHeight = MAX(MIN(textHeight, 80.0), 33.0);

        chatTxtHeightConstraint.constant = newViewHeight;

        [textView scrollRangeToVisible:textView.selectedRange];
    }
}

Any solution?

Upvotes: 1

Views: 73

Answers (1)

Sunil Zalavadiya
Sunil Zalavadiya

Reputation: 1993

I have solved issue by writing [textView layoutIfNeeded]; and [textView updateConstraints]; bellow code line of chatTxtHeightConstraint.constant = newViewHeight;

- (void)textViewDidChange:(UITextView *)textView
{
    if([textView contentSize].height <80.0)
    {
        CGFloat textHeight         = [self textHeightForTextView:textView];

        CGFloat newViewHeight = MAX(MIN(textHeight, 80.0), 33.0);

        chatTxtHeightConstraint.constant = newViewHeight;

        [textView layoutIfNeeded];
        [textView updateConstraints];

        [textView scrollRangeToVisible:textView.selectedRange];
    }
}

Upvotes: 1

Related Questions