Showing posts with label UITextField Delegate Methods for Show Hide Keyboard with Animation.. Show all posts
Showing posts with label UITextField Delegate Methods for Show Hide Keyboard with Animation.. Show all posts

Tuesday, October 5, 2010

UITextField Delegate Methods for Show Hide Keyboard with Animation.

 These delegate methods will automatically call according to the trigger.
Code for .h Controller. 


@interface UIController : UIViewController <UITextFieldDelegate>{

}

-(void)setViewMovedUp:(BOOL)movedUp;

Code for .m Controller.
 
- (void)viewWillAppear:(BOOL)animated{
   
    // watch the keyboard so we can adjust the user interface if necessary.
    [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(keyboardWillShow:)
                                                  name:UIKeyboardWillShowNotification object:self.view.window];
    [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(keyboardWillHide:)
                                                  name:UIKeyboardWillHideNotification 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];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - UIViewController delegate methods

- (void) keyboardWillShow:(NSNotification *)notif{
    // The keyboard will be shown. If the user is editing the comments, adjust the display so that the
    // comments field will not be covered by the keyboard.
    if((txtCity.editing==TRUE) || (txtTwitterID.editing==TRUE) || (txtTwitterPassword.editing==TRUE) || (txtFacebookID.editing==TRUE) || (txtFacebookPassword.editing==TRUE) || (txtPostalCode.editing == TRUE) || (txtState.editing == TRUE) || (txtCountry.editing == TRUE) || (txtTeamName.editing==TRUE)){
       
    }
    else {
        [self setViewMovedUp:YES];
    }
}

- (void) keyboardWillHide:(NSNotification *)notif{
    // The keyboard will be hidden.
    // view should be shifted donw to its real position.
    [self setViewMovedUp:NO];
}

-(void)textViewDidBeginEditing:(UITextView *)textView{
   
    [self setViewMovedUp:YES];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
   
    if([text isEqualToString:@"\n"]) {
        [txtViewDescription resignFirstResponder];
        [self setViewMovedUp:NO];
        return NO;
    }   
    return NO;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    // The keyboard will be shown. If the user is editing the comments, adjust the display so that the
    // comments field will not be covered by the keyboard.
    if((txtCity.editing==TRUE) || (txtTwitterID.editing==TRUE) || (txtTwitterPassword.editing==TRUE) || (txtFacebookID.editing==TRUE) || (txtFacebookPassword.editing==TRUE) || (txtPostalCode.editing == TRUE) || (txtState.editing == TRUE) || (txtCountry.editing == TRUE) || (txtTeamName.editing==TRUE)){
       
    }
    else {
        [self setViewMovedUp:YES];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{   
    [txtCity resignFirstResponder];
    [txtTwitterID resignFirstResponder];
    [txtPostalCode resignFirstResponder];
    [txtTwitterPassword resignFirstResponder];
    [txtState resignFirstResponder];
    [txtFacebookID resignFirstResponder];
    [txtFacebookPassword resignFirstResponder];
    [txtCountry resignFirstResponder];
    [txtAdministratorName resignFirstResponder];
    [txtFlickrID resignFirstResponder];
    [txtAdminEmailID resignFirstResponder];
    [txtFlickrPassword resignFirstResponder];
    [txtNumberOfDivisions resignFirstResponder];
    [txtLevels resignFirstResponder];
    [self setViewMovedUp:NO];
    return NO;
}

- (void)setViewMovedUp:(BOOL)movedUp{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    // Make changes to the view's frame inside the animation block. They will be animated instead
    // of taking place immediately.
    CGRect rect = self.view.frame;
    if (movedUp){       
        if(rect.origin.y == 0)
            rect.origin.y = self.view.frame.origin.y - 216;
    }
    else{       
        if(rect.origin.y < 0)
            rect.origin.y = self.view.frame.origin.y + 216;
    }   
    self.view.frame = rect;   
    [UIView commitAnimations];
}