15

Before I was using this method....

//TextView is a UITextView 

[TextView scrollRangeToVisible:NSMakeRange([TextView length], 0)];

...which would programmatically scroll to the end of the UITextView but it doesn't seem to be working in iOS 4.0. Is there a way to programmatically scroll to the end of a UITextView without changing editablility or inserting a point (where the user can tap on the UITextView and have a keyboard show up)?

Also, do I need to assign the file owner as the delegate? Does it make a difference?

TechZen
  • 64,370
  • 15
  • 118
  • 145
OscarTheGrouch
  • 2,374
  • 4
  • 28
  • 39
  • What do you mean by "not working". Does it scroll at all? Is the keyboard being evoked? – TechZen Jul 03 '10 at 13:28
  • When using [TextView scrollRangeToVisible:NSMakeRange([TextView.text length], 0)]; It does not scroll at all, but it does not change the .editable of the textview (which is good for me). A sample project demonstrating this is at http://dl.dropbox.com/u/8256776/Bugsy.zip it is a ViewController template with a UITextView and two buttons. when one button is pressed it adds "\nFive" to the UITextView and attempts to scroll down to the end (unsuccessfully). when the other button is pressed it adds "\nSix" to the UITextView and attempts to scroll down to the end (unsuccessfully). – OscarTheGrouch Jul 03 '10 at 19:27
  • what do you mean by unsuccessfully? Are you testing this on the simulator? I see the textview scrolling to the bottom... If I scroll it to the top, when I press five or six it scrolls to the bottom. It is working perfectly. – Duck Jan 25 '11 at 02:21

5 Answers5

37

UITextView doesn't have length property. Following code works good for my environment.

[TextView scrollRangeToVisible:NSMakeRange([TextView.text length], 0)];
ashphy
  • 524
  • 5
  • 5
6

The answer didn't work for me, but following what you would use for a TableView works perfect. Just make sure your UITextView is named textView.

if (textView.contentSize.height > textView.frame.size.height)
{
    CGPoint offset = CGPointMake(0, textView.contentSize.height - textView.frame.size.height);
    [self.textView setContentOffset:offset animated:YES];
}
Sam
  • 390
  • 1
  • 3
  • 15
4

In IOS8, a call to ensureLayoutForTextContainer seems to make this solution work.. Took me nearly an hour to track this down.

    logObject.layoutManager.ensureLayoutForTextContainer(logObject.textContainer)

    logObject.setContentOffset(CGPointMake(0.0, logObject.contentSize.height), animated:false)
Jeremy Gilbert
  • 151
  • 1
  • 6
1

This worked for me:

[_myTextView.layoutManager ensureLayoutForTextContainer:_myTextView.textContainer];
[_myTextView scrollRangeToVisible:NSMakeRange([_myTextView.text length], 0)];
sp00ky
  • 151
  • 9
-1

This is what I use it works fine. The shouldScrollTextToBottom is set by the calling view (1 view controller lower in the calling stack)

(void)viewDidAppear:(BOOL)animated 
{ // scroll to bottom if required
  if(shouldScrollTextToBottom)
    [txtMyTextView scrollRectToVisible:CGRectMake(0, 0, txtMyTextView.frame.size.width, txtMyTextView.frame.size.height * 6) animated:YES];  
}

6 is an arbitrarily large number which should be a multiple of the UITextView's height. I have found that with a value of 5, my view does not scroll to the absolute bottom.

Anjaan
  • 660
  • 6
  • 10