iOS4: Can't Programmatically Scroll to Bottom of UITextView
Asked Answered
R

5

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?

Remy answered 3/7, 2010 at 13:2 Comment(3)
What do you mean by "not working". Does it scroll at all? Is the keyboard being evoked?Headrace
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 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).Remy
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.Mazard
T
37

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

[TextView scrollRangeToVisible:NSMakeRange([TextView.text length], 0)];
Tole answered 24/2, 2011 at 6:0 Comment(0)
T
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];
}
Truncate answered 14/11, 2012 at 13:8 Comment(2)
Yes! This works perfectly (and I've tried all kinds of variations of scrollRangeToVisible in my app!)Dinger
This seemed to work better than the other, accepted answer! ThanksCom
M
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)
Mendelism answered 13/10, 2014 at 22:16 Comment(0)
K
1

This worked for me:

[_myTextView.layoutManager ensureLayoutForTextContainer:_myTextView.textContainer];
[_myTextView scrollRangeToVisible:NSMakeRange([_myTextView.text length], 0)];
Knoxville answered 9/9, 2015 at 14:4 Comment(0)
J
-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.

Journalistic answered 17/11, 2012 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.