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?
UITextView doesn't have length property. Following code works good for my environment.
[TextView scrollRangeToVisible:NSMakeRange([TextView.text length], 0)];
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];
}
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)
This worked for me:
[_myTextView.layoutManager ensureLayoutForTextContainer:_myTextView.textContainer];
[_myTextView scrollRangeToVisible:NSMakeRange([_myTextView.text length], 0)];
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.
精彩评论