Except for the top text field,the iphone keyboard hides all the remaining textfields when I start typing.How do I show the active textfield above the keyboard and then bring it back to normal when keyboard hides?
-
can you please paste code here? – Nimit Parekh Mar 19 '13 at 07:19
-
Move the entire view up when the virtual keyboard pops up. See this one. http://stackoverflow.com/questions/15145341/undock-keyboard-programmatically – El Tomato Mar 19 '13 at 07:32
4 Answers
Watch for UIKeyboardWillShowNotification
and UIKeyboardWillHideNotification
. The NSNotification
objects delivered with these notifications will have userInfo dictionaries that contain information about the frame that the keyboard will have after it finishes animating. Adjust your layout in response to these notifications to keep the things you want from being covered by the keyboard.

- 5,949
- 9
- 41
- 52

- 7,092
- 1
- 25
- 31
Use the following code before that place all your textfield in scrollview.
#define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:mailTf])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}

- 5,262
- 4
- 38
- 68
-
-
The code works for text fields but does'nt work for the uitextview which is at the bottom.Please help – zzzzz Mar 19 '13 at 07:38
-
Put your UITextField
in UIScrollView
and use contentOffSet
property of UIScrollView
like this:
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == txt1) //
{
[txt1 becomesFirstResponder];
scrMainView.contentOffset = CGPointMake(0, 20);
}
else if (textField == txt2) //
{
[txt1 resignFirstResponder];
[txt2 becomesFirstResponder];
scrMainView.contentOffset = CGPointMake(0, 60);
}
//And so on..
return YES;
}
Hope, you'll get answer.
Thanks.

- 5,772
- 5
- 45
- 68

- 2,745
- 2
- 30
- 50
-
What is `scrMainView`? Mention proper details otherwise user will not come to know what you're suggesting. `scrMainView` is probably scollview right? – rohan-patel Mar 19 '13 at 09:45
-
yea. scrMainView is my scrollview, so it'll set content offset to little bit upper so, user can interact easily. – Manann Sseth Mar 19 '13 at 09:47
-
Please provide those details then. Just have a look of my edit. It will give clear idea of your answer. – rohan-patel Mar 19 '13 at 09:48
-
Also I highly doubt if you could compare two `UITextFields` like this: `if (textField == txt1)` – rohan-patel Mar 19 '13 at 09:53
-
1becomeFirstResponder and not becomesFirstResponder, remove the 's' – user2167877 Mar 26 '15 at 20:35
You have to use a scrollview on top of your view and implement mathemetical contentOffset setting according to what textfield you choose. The textfield you choose can be obtained from the delegate methods and the calculation of offset can be done with textfield frame

- 19,955
- 12
- 56
- 101