Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh UITextView's Scroll Position on iPhone

I'd like to do some type of refresh of a UITextView to set the textview back to it's original state. I have a paragraph that gets dynamically populated depending on which TableViewCell the user clicks on. So when they scroll the text field, then go back and select another cell and return, the text changes, but the scroll position remains as the user left it. How can I return it to its default state. Thanks!

like image 478
angelfilm entertainment Avatar asked Aug 27 '09 21:08

angelfilm entertainment


5 Answers

By default state, do you mean scrolled to the top? If so, you're on the right track. Try

[myTextView scrollRangeToVisible:NSMakeRange(0, 0)];
like image 107
Shaggy Frog Avatar answered Nov 15 '22 02:11

Shaggy Frog


I've found that if I clear the UITextView first and then apply the new text, it will automatically "scroll" back to the top.

myTextView.text = @"";
myTextView.text = theRealTextContent;
like image 34
Jay Avatar answered Nov 15 '22 03:11

Jay


This works if you want it to scroll up to the top:

[theTextView scrollRangeToVisible:NSMakeRange(0, 0)];

The only problem is that this animates its way up. If you want it to snap to the top, use this:

[theTextView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
like image 6
Adam Avatar answered Nov 15 '22 01:11

Adam


[textview setScrollEnabled:YES];
textview.text = yourText;
[textview setScrollEnabled:NO];
like image 5
canzhiye Avatar answered Nov 15 '22 01:11

canzhiye


Neither one of these approaches worked satisfactorily for me.

  • scrollRangeToVisible produces tons of visual artifacts (lots of scrolling when switching between large strings), and in fact only works at all in a delayed call some time (I used 0.1 seconds) after the contents has been changed, not directly as Shaggy Frog's answer implies.

  • setting the text to @"" before setting the new contents didn't help, even with a delay.

It seems as if once a UITextView has been typed in, it forever is in this mode where setting new contents causes annoying scrolling artifacts. I tried setting the selectionRange to the beginning of the UITextView as well, but that didn't help. Sending the resignFirstResponder message before setting the new contents didn't help, either.

Because of this, I decided to delete and recreate the UITextView each time I change the contents. That's a rare enough event (based on human interaction) in my app that it's not a performance problem. It was the only way I could find to "load new contents" into the UITextView without tons of annoying scrolling artifacts.

My experience is with OS3.2 (ipad simulator)

like image 1
Bogatyr Avatar answered Nov 15 '22 03:11

Bogatyr