-3

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?

zzzzz
  • 1,209
  • 2
  • 18
  • 45

4 Answers4

1

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.

Venk
  • 5,949
  • 9
  • 41
  • 52
Aaron Golden
  • 7,092
  • 1
  • 25
  • 31
1

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];
}
Vinodh
  • 5,262
  • 4
  • 38
  • 68
1

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.

rohan-patel
  • 5,772
  • 5
  • 45
  • 68
Manann Sseth
  • 2,745
  • 2
  • 30
  • 50
0

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

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101