0

I was following the code here

#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];

    return YES;

}

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if ([sender isEqual:password])
    {
        //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.5]; // 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)keyboardWillShow:(NSNotification *)notif
{
    //keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately

    if ([password isFirstResponder] && self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (![password isFirstResponder] && self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}


- (void)viewWillAppear:(BOOL)animated
{
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification object:self.view.window]; 
}

- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
}

It actually moves the view up when I tap on the password UITextField, however after I finish it did not scroll down again. What am I missing here?

Community
  • 1
  • 1
aherlambang
  • 14,290
  • 50
  • 150
  • 253
  • So the keyboardWillShow: is both for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification notifications? – SVD Apr 06 '11 at 03:33
  • Are you assuming that when the password field resigns first responder that the other text field will necessarily become first responder? I.e. I'm wondering if the keyboardWillShow method is even being called in this case. – Rayfleck Apr 06 '11 at 03:34
  • it is actually called... if not the view won't move up, which is what I have – aherlambang Apr 06 '11 at 04:12
  • what does the first responder mean actually? – aherlambang Apr 06 '11 at 04:18

3 Answers3

1

Try

-(void)textFieldDidEndEditing:(UITextField *)sender
{
     // Your code to scroll down your view.
}

It written and not tested yet.

swapnil
  • 25
  • 2
1

You implemented keyboardWillShow. You also have to implement keyboardwillHide.

Check this post:

Making a UITableView scroll when text field is selected

This worked great for me.

Community
  • 1
  • 1
Xiao
  • 873
  • 6
  • 10
0

I'm not sure how to fix your code issue. But, there is a easy way. You may wanna try the highly recommended "TPKeyboardAvoidingScrollView": https://github.com/michaeltyson/TPKeyboardAvoiding

Yi Jiang
  • 3,938
  • 6
  • 30
  • 62