-2

I have a problem i have a 15 text fields in a table view.when i am entering text to last text field it is covering by key board.Any one please give me a solution for this.

My textfields are in a detail view controleer of split view in IPAD,

Thanks in advance for your solution

sudheer
  • 418
  • 6
  • 20

2 Answers2

0

You can add these lines of coding:

//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.3]; // if you want to slide up the view

CGRect rect = self.view.frame;
if (movedUp)
{
    NSLog(@"MOVE-UP");
    // 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
{
    NSLog(@"MOVE-DOWN");
    // 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
{
   // Animate the current view out of the way
   if (self.view.frame.origin.y >= 0)
   {
       [self setViewMovedUp:YES];
   }
   else if (self.view.frame.origin.y < 0)
   {
      [self setViewMovedUp:NO];
   }
 }


 -(void)keyboardWillHide
 {
    if (self.view.frame.origin.y >= 0)
    {
       [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
       [self setViewMovedUp:NO];
    }
 }

And in viewDidAppear:

 -(void)viewDidAppear:(BOOL)animated
 {

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

 }

you have to define this also #define kOFFSET_FOR_KEYBOARD 100.0//as you want

Ajay Chaudhary
  • 1,993
  • 15
  • 23
  • Thank u so much for your answer.But it is not working foe two views which are added to tab bar.In my program my two views are added to tab bar.your code is working for one view.If i added to another view also it is not working properly.......Please give me a solution for this. – sudheer May 22 '13 at 07:11
  • I heve another doubt...Is it work for uitextview also......Because your code is not workin for textview and after usiing textview text field is not working properly the code......Please give me a solution please – sudheer May 22 '13 at 07:29
  • There is no need to resize the self.view , it can easily be achievable by resizing your controller like UITableView or UITextView – Dushyant Singh May 22 '13 at 07:49
0

//call when textfield editing started

-(void)textFieldDidBeginEditing:(UITextField *)textField{

        [self animateTextField: indexPath up: YES];

}

//Text Field after editing ended

  • (void)textFieldDidEndEditing:(UITextField *)textField{

    [self animateTextField: indexPath up: NO];
    

}

//code for animateTextField .The movement Distance will change as per requirement

  • (void) animateTextField: (NSIndexPath*) indexPath up: (BOOL) up{

    const int movementDistance =indexPath.section==0?20: 180;

    const float movementDuration = 0.3f;

    int movement = (up ? -movementDistance : movementDistance);

    NSLog(@"movement:%d",movement);

    [UIView beginAnimations: @"aimation" context: nil];

    [UIView setAnimationBeginsFromCurrentState: YES];

    [UIView setAnimationDuration: movementDuration];

    self.view.frame= CGRectOffset(self.view.frame,0, movement);

    [UIView commitAnimations];

}

sudheer
  • 418
  • 6
  • 20