Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Scroll UITextView when keyboard appers

Hi,

is it possible to scroll a UITextView to the last line when the keyboard appers?
I have a editable UITextView which gets half covered by the keyboard. So I want to make the UITextView scroll up to the last line of text when the keyboard appears so that the user can add more text without it being covered.
I searched for this on google and on this forum but I couldn't find a solution.
I would be happy if someone could help me here! Thanks!

MacBook Pro, Mac OS X (10.5.5)

Posted on Nov 18, 2008 12:52 PM

Reply
7 replies

Jan 4, 2009 8:53 AM in response to freshking

Hi, I'm sure you've found a solution by now, but you never know:

Did you try:


- (void)keyboardWillShow:(NSNotification *)notif
{
// The keyboard will be shown. If the user is editing the author, adjust the display:
[myTextView scrollRangeToVisible:NSMakeRange([myTextView.text length], 0)];

}
- (void)viewWillAppear:(BOOL)animated
{
// watch the keyboard
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self setEditing:NO animated:YES];

// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}


??

Jan 5, 2009 6:08 PM in response to Ostrowsky S

hey, I tried that and it does help but there seem to be other issues when trying to do this with a text view versus, say, a text field. It does auto scroll to the defined point but it doesn't account for the keyboard being in view and therefore the keyboard is still blocking half the view.

I tried using a UIView animation block to move the view up or down at the same time the "scrollRangeToVisible" is called and things got even better but now since I moved the view up, I can't manually scroll to the top anymore since I pushed it out of view to make up for the keyboard.

I also noticed (I don't know, maybe i'm doing something wrong) that when your text view has just enough characters to fill up most of the view but not go beyond the view, the text view is not 'scrollable', so therefore words hidden by the keyboard are unreachable for sure.

A UITableView is still somewhat 'scrollable' even if it's not filled with visible cells so I would think a text view should work the same way since they both inherit from UIScrollView.

Any suggestions??

Jan 5, 2009 6:28 PM in response to bones237

I'm sure there are tons of reasons why this is a bad idea (the hard-coded dimensions feel like the Wrong Thing to do), but it works for me. I'm resizing the textview when the keyboard appears and disappears. I also slap a "Done" button up on the view's navigation item; this button hides the keyboard.


- (void)textViewDidBeginEditing:(UITextView *)textView
{
// provide my own Save button to dismiss the keyboard
UIBarButtonItem* saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(dismissKeyboard:)];
self.navigationItem.rightBarButtonItem = saveItem;
[saveItem release];
}
- (void)dismissKeyboard: (id) sender
{
[myTextView resignFirstResponder];
self.navigationItem.rightBarButtonItem = nil;
}
- (void)keyboardDidShow:(NSNotification *)note {
CGRect r = CGRectMake (0, 48, 320, 149);
[myTextView setFrame: r];
}
- (void)keyboardWillHide:(NSNotification *)note {
CGRect r = CGRectMake (0, 48, 320, 323);
[myTextview setFrame: r];
}

Jan 5, 2009 7:44 PM in response to jason.priebe

yeah that worked good... everything is reachable when the keyboard shows, thanks!!

the only thing I added was I put the part where the frame of the text view is shrunk when the keyboard shows inside a animation block. It made the transition nice and smooth instead of instant.


- (void)keyboardWillShow:(NSNotification *)notif {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

CGRect r = CGRectMake (0, 0, 320, 200);
[myTextView setFrame: r];

[UIView commitAnimations];

}

Jun 2, 2009 2:10 AM in response to Ostrowsky S

Since we are dealing with a UITextView, why wouldn't just set the UIViewController as a <UITextViewDelegate> and then use these methods for getting to know when the keyboard appears/disappears:

<pre>
- (void)textViewDidBeginEditing:(UITextView *)textView {
// keyboard appeared
}

- (void)textViewDidEndEditing:(UITextView *)textView {
// keyboard disappeared
}
</pre>

?


PS: I've searched in the FAQ how to format the code in this post, but I couldn't find anything ( http://discussions.apple.com/help.jspa#format).

Jun 22, 2009 8:39 AM in response to freshking

The topic is to some extend covered in the developer documentation:
http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhone OSProgrammingGuide/TextandWeb/TextandWeb.html

There you find information on how to get the size of the keyboard (hence avoiding the hardcoded coordinates in the some of the sample code above).

My solution of choice woud be to resize the UITextView/UIScrollView to the visible area (the document above describes how to do it) and then scroll to the position of interest...

Scroll UITextView when keyboard appers

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.