animate the textfield to top when it is clicked to type a text from keypad

73 views Asked by At

I am new to iOS, just started to design an login form. when user click the textfield, keypad appears which hide the textfield so I unable to see the textfield. So how I animate the textfield to top at the time of keypad appear.

1

There are 1 answers

0
AudioBubble On

You can try this->

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == ChatField)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

    }return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField == ChatField) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
    }return YES;
}


- (void)keyboardWillShow:(NSNotification *)notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    float newVerticalPosition = -keyboardSize.height + 10;

    [self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f];
}


- (void)keyboardWillHide:(NSNotification *)notification
{
    CGFloat  kNavBarHeight =  self.navigationController.navigationBar.frame.size.height;
    float newVerticalPosition = kNavBarHeight  -45;

    [self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f];

}


- (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration {
    CGRect frame = self.view.frame;
    frame.origin.y = position;

    [UIView animateWithDuration:duration animations:^{
        self.view.frame = frame;
    }];
}