views:

1545

answers:

4

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!

A: 

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

[myTextView scrollRangeToVisible:NSMakeRange(0, 0)];
Shaggy Frog
I did try that as well, but it didn't seem to go to the real top of the text. It only get's close to the top, but doesn't look like the default. Honestly I don't really have a good understanding of the NSRange stuff.
Angelfilm Entertainment
I just tried this with an app I have, and it works for me. Can you post a screenshot of your simulator so we can see the behaviour?
Shaggy Frog
Angelfilm Entertainment
Can you manually make it scroll up to the top with your finger?
Shaggy Frog
yes I can. I have full control of it.
Angelfilm Entertainment
If you set a breakpoint for the line of code in my answer, does it get hit when you reload this view?
Shaggy Frog
+1  A: 

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;
Jay
A: 

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)

Bogatyr
+1  A: 

[textview setScrollEnabled:YES]; textview.text = yourText; [textview setScrollEnabled:NO];

canzhiye