Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS4: Can't Programmatically Scroll to Bottom of UITextView

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?

like image 972
OscarTheGrouch Avatar asked Jul 03 '10 13:07

OscarTheGrouch


3 Answers

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

[TextView scrollRangeToVisible:NSMakeRange([TextView.text length], 0)];
like image 55
ashphy Avatar answered Nov 02 '22 16:11

ashphy


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];
}
like image 30
Sam Avatar answered Nov 02 '22 16:11

Sam


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)
like image 4
Jeremy Gilbert Avatar answered Nov 02 '22 16:11

Jeremy Gilbert