Managing the view of an iOS application with respect to the keyboard events generated by iOS application is one of the most widely faced issue by most of the iOS developers. I have found a very simple and efficient code for handling keyboard events for an iOS application when the keyboard is displayed and hidden again. I am sharing this code with you. Hope it would be helpful to you too.
Here is sample code to handle the keyboard height iOS programmatically:
- (void)registerForKeyboardNotifications  { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWasShown:(NSNotification*)aNotification { 
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
   
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    main.contentInset = contentInsets;
    main.scrollIndicatorInsets = contentInsets;
   
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
   
    CGPoint field_size=activeField.frame.origin;
    field_size.y+=activeField.frame.size.height;
   
    if (!CGRectContainsPoint(aRect, field_size) ) {
        [main scrollRectToVisible:activeField.frame animated:YES];
    }
    info=nil;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification { 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    main.contentInset = contentInsets;
    main.scrollIndicatorInsets = contentInsets;
}
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField { 
    main.scrollEnabled=YES;
    activeField = textField;
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField { 
    NSInteger nextTag = textField.tag + 1;
    // Try to find next responder
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
    if (nextResponder) {
        // Found next responder, so set it.
        activeField =((UITextField *)[textField.superview viewWithTag:nextTag]);
        [nextResponder becomeFirstResponder];
    } else {
        // Not found, so remove keyboard.
        [textField resignFirstResponder];
    }
    return NO; // We do not want UITextField to insert line-breaks.
}
-(void) textFieldDidEndEditing:(UITextField *)textField { 
    activeField = nil;
    main.scrollEnabled=NO;
}
In the above code activeField is an UITextField that is used by me to point to the current text field on focus and main is a scroll view added on top the self view on which I have added all the text fields as you can see in the above code. For any issues feel free to post the comment below. I will be happy to help you.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.