In previous versions of iOS, my UITextView
will scroll to the bottom using
[displayText scrollRangeToVisible:NSMakeRange(0,[displayText.text length])];
or
CGFloat topCorrect = displayText.contentSize.height -[displayText bounds].size.height;
topCorrect = (topCorrect<0.0?0.0:topCorrect);
displayText.contentOffset = (CGPoint){.x=0, .y=topCorrect};
But the former will now have the weird effect of starting at the top of a long length of text and animating the scroll to the bottom each time I append text to the view. Is there a way to pop down to the bottom of the text when I add text?
For future travelers, building off of @mikeho's post, I found something that worked wonders for me, but is a bit simpler.
1) Be sure your
UITextView
'scontentInset
s are properly set & your textView is alreadyfirstResponder()
before doing this.2) After my the insets are ready to go, and the cursor is active, I call the following function:
I believe this is a bug in iOS 7. Toggling scrollEnabled on the UITextView seems to fix it:
I think your parameters are reversed in
NSMakeRange
. Location is the first one, then how many you want to select (length)....would create a selection starting with the 0th (first?) character and going the entire length of the string. To scroll to the bottom you probably just want to select a single character at the end.
This is working for me in iOS SDK 7.1 with Xcdoe 5.1.1.
I do this as I add text programmatically, and the text views stays at the bottom like Terminal or command line output.
The best way is to set the bounds for the
UITextView
. It does not trigger scrolling and has an immediate effect of repositioning what is visible. You can do this by finding the location of the caret and then repositioning:This really works for me in iOS 7.1.2.