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!
By default state, do you mean scrolled to the top? If so, you're on the right track. Try
[myTextView scrollRangeToVisible:NSMakeRange(0, 0)];
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;
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];
[textview setScrollEnabled:YES];
textview.text = yourText;
[textview setScrollEnabled:NO];
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With