scrollRangeToVisible: How to scroll to the beginning of an UITextView
The range you're creating starts at -1! You can create your range as follows, it's easier:
NSRange range = NSMakeRange(0, 1);
This range starts at an index of 0 (first character) and spreads over 1 characters.
n.evermind
I speak: Copy-and-paste, Basic, Objective-C, Python, Django and have recently learned how to use the very retro but stylish terminal. Otherwise: Creator of digital-analog-but-stunningly beautiful notebooks in love with beautiful UIs
Updated on June 04, 2022Comments
-
n.evermind almost 2 years
A beginner's headache: I am trying to scroll to the very top of my UITextView once the keyboard is dismissed. I had tried to extract an answer from here, but I'm afraid it didn't help much.
I thought I do this with scrollRectToVisible, but nothing happens. Then I thought I should try scrollRangeToVisible, but this crashed my app... I'm sure I've done something tremendously upsetting and wrong. I'd be very glad if somebody could help:
- (IBAction)hideKeyboard:(id)sender { //[textView scrollRectToVisible:CGRectMake(0, 0, 0, 0) animated:YES]; NSRange range = NSMakeRange(textView.text.length - (textView.text.length+1),1); [textView scrollRangeToVisible:range]; textView.scrollEnabled = NO; [textView resignFirstResponder];}
EDIT:
updated code for anyone who encounters a similar problem:
- (IBAction)hideKeyboard:(id)sender { //textView.scrollEnabled = NO; [textView resignFirstResponder]; NSRange range = NSMakeRange(0,1); [textView scrollRangeToVisible:range];
}