Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth UITextView auto scroll to bottom of frame

Im using Xcode 5.1.1 developing for ios7. As there is new text entering the UITextView, I would like it if the text went up, leaving room for the user to see the new text. I have something that works but the animation that shows the new text is awkward. Its almost as if it goes from the very top of the text and quickly goes to the bottom every time its called.

CGPoint p = [textview contentOffset]; [textview setContentOffset:p animated:NO]; [textview scrollRangeToVisible:NSMakeRange([textview.text length] - 1,0)];

This code is getting called every time new text is entered. I would like it to be as smooth as the default iPhone messenger where it just slides up casually.

like image 821
Alessandro Kreslin Avatar asked Jun 27 '14 13:06

Alessandro Kreslin


2 Answers

The RIGHT answer is to set:

_consoleView.layoutManager.allowsNonContiguousLayout = NO;

In ViewDidLoad

Then:

_consoleView.text = text;
[_consoleView scrollRangeToVisible:NSMakeRange(_consoleView.text.length - 1, 1)];
like image 57
mikemeli Avatar answered Oct 21 '22 12:10

mikemeli


(Answered by a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

Solution: The problem was that when text was getting inserted into the texview it scrolled to the top then called the scrollRangeToVisible which scrolled it to the bottom, which gave a bad animation and an unpleasant user experience. I solved this by disabling scroll before the text enters, and enabled it after the text was entered so it only calls the scrollRangeToVisible

[textview scrollRangeToVisible:textview.selectedRange];
textview.scrollEnabled= NO;
textview.text = [textview.text stringByAppendingString:createdString];
textview.scrollEnabled= YES;
like image 34
2 revs Avatar answered Oct 21 '22 12:10

2 revs