-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

4

2 回答 2

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

于 2013-05-22T06:53:01.187 回答
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];

}

于 2013-06-05T05:25:50.897 回答