3

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];

}

Community
  • 1
  • 1
n.evermind
  • 11,944
  • 19
  • 78
  • 122

2 Answers2

5

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.

James Bedford
  • 28,702
  • 8
  • 57
  • 64
  • Thanks for your swift reply. It now doesn't crash anymore, but it actually doesn't scroll to the very beginning, but rather puts the beginning of the UITextView in the middle of my Screen, i.e. @ pixel 250 or so. – n.evermind Mar 11 '11 at 13:41
  • 2
    Sorry, just solved it myself. I need to call the scrollRangeToVisible AFTER the resigning the keyboard. Sorry for bothering. – n.evermind Mar 11 '11 at 13:48
  • Awesome! Pleased you sorted it. :) – James Bedford Mar 11 '11 at 14:14
  • I'm afraid, but I just noticed another bug with this. If I enter something in my UITextView and then resign the keyboard, everything is fine (the scrolling happens and I'm happy). However, if I DON'T do anything with my UITextView and try to resign the keyboard, the app CRASHES. The bug is clearly due to the scrollRangeToVisible method, because if I comment that out, everything is fine (no crashes, but no scrolling either). Do I have to initiate the range perhaps? or the textView? – n.evermind Mar 11 '11 at 15:24
  • Would it perhaps not be better to simply scroll to a fixed point instead of first character which spreads over one character? – n.evermind Mar 11 '11 at 15:25
-1

i found this answer ,about scrolling textView to top, before here in this forum ,i did'nt find it now .But this was the answer: You just use this method to stop scroll:

- (void)killScroll :(UIScrollView*)scrollView 
{
    CGPoint offset = scrollView.contentOffset;
    offset.x -= 1.0;
    offset.y -= 1.0;
    [scrollView setContentOffset:offset animated:NO];
    offset.x += 1.0;
    offset.y += 1.0;
    [scrollView setContentOffset:offset animated:NO];
}

and after calling it ,you use the famous method:

[yourTextView scrollsToTop];
gak
  • 32,061
  • 28
  • 119
  • 154
Mejdi Lassidi
  • 999
  • 10
  • 22