0

I have a form on the ipad which has many textfield and a button at the end. There are some fields which come under the keyboard when it is active. In order to pull the hidden texfield behind the keyboard to be visible I am using the following code.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
_scrollView.frame=CGRectMake(0, 0, 1024, 655);
[self animateTextField:textField up:NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
CGPoint temp = [textField.superview convertPoint:textField.frame.origin toView:nil];
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait)
{
    // NSLog(@"portrait");
    if(up)
    {
        int moveUpValue = temp.y+textField.frame.size.height;
        animatedDis = 264-(1024-moveUpValue-5);
    }
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
{
    if(up)
    {
        int moveUpValue = 1004-temp.y+textField.frame.size.height;
        animatedDis = 264-(1004-moveUpValue-5);
    }
}
else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
    if(up)
    {
        int moveUpValue = temp.x+textField.frame.size.height;
        animatedDis = 352-(768-moveUpValue-5);
    }
}
else
{
    if(up)
    {
        int moveUpValue = 768-temp.x+textField.frame.size.height;
        animatedDis = 352-(768-moveUpValue-5);
        _scrollView.frame = CGRectMake(0, 0, 1024, 655-240);
    }

}
if(animatedDis>0)
{
    const int movementDistance = animatedDis;
    const float movementDuration = 0.3f;
    int movement = (up ? -movementDistance : movementDistance);
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    if (orientation == UIInterfaceOrientationPortrait)
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    else if(orientation == UIInterfaceOrientationLandscapeLeft)
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    else
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    [UIView commitAnimations];
}
}

I am also using the scroll view. My issue is that when the keyboard is active and i press my button it take me to the next screen. Now if i navigate back, the keyboard is active and the prior animations are set. Now if i hide the keyboard, the entire view scrolls down leaving a black portion on top. How to handle this situation?

southpark
  • 541
  • 2
  • 5
  • 19

1 Answers1

0

Okay. After much research I found a simple method. It is the use of notifications. In my viewDidLoad i added these two keyboard notifications.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name: UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];

These are the 2 selector methods:

-(void)keyboardWasShown:(NSNotification *)aNotification
{

if (displayKeyboard==YES) {
    return;
}
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
//NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;

NSLog(@"kbw====%fkbh====%f",keyboardSize.width,keyboardSize.height);

offset = _scrollView.contentOffset;

CGRect viewFrame = _scrollView.frame;

NSLog(@"svw====%fsvh===%f",viewFrame.size.width,viewFrame.size.height);
viewFrame.size.height -= keyboardSize.height-49;
NSLog(@"new view hgt =====%f",viewFrame.size.height);
_scrollView.frame = viewFrame;

CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[_scrollView scrollRectToVisible: textFieldRect animated:YES];
displayKeyboard = YES;
}
-(void)keyboardWillBeHidden:(NSNotification *)aNotification
{

if (!displayKeyboard) {
    return; 
}

_scrollView.frame = CGRectMake(0, 0, 1024, 655);
_scrollView.contentOffset =offset;
displayKeyboard = NO;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}

displayKeyboard, offset and activeField are declared in .h file.

Also remember to remove the notifications in viewDidDisappear:animated

Although this method is quite different than the previous one stated, this one does not leave a black portion on the top while navigating between classes when the uikeyboard is active.

Also what i noticed was if i used the deprecated UIKeyboardBoundsUserInfoKey i used to get the correct width and height of the keyboard(i am working only in landscape mode). Whereas when i used UIKeyboardFrameBeginUserInfoKey the width and height values were interchanged. I am still trying to figure out this problem.

Also when the keyboard used to appear, a fixed space of 49px was appended to above it. I assumed that that was my tabbar height and therefore subtracted 49.

southpark
  • 541
  • 2
  • 5
  • 19