keyboard is hiding the text field in IOS

Viewed 13967

I have created a form in IOS using scrollview with some text fields. the view looks like this,

view of form

When I begin to edit either state or below fields, the keyboard hides that field. like this,

enter image description here

what should i do to see the below fields (i.e., state and below)??

5 Answers

You should definitely search for this in stack-overflow itself, before posting a question. Anyways, you can find the answer here.

  1. Conform to protocol UITextFieldDelegate

  2. Have a BOOL variable moved to signal, whether your view is moved or not.

  3. Then implement the delegate methods.

    -(void)textFieldDidBeginEditing:(UITextField *)textField {
        if(!moved) {
         [self animateViewToPosition:self.view directionUP:YES];
        moved = YES;
      }
    }
    
    -(void)textFieldDidEndEditing:(UITextField *)textField {
     [textField resignFirstResponder];
    }
    
    -(BOOL)textFieldShouldReturn:(UITextField *)textField {
     [textField resignFirstResponder];
      if(moved) {
         [self animateViewToPosition:self.view directionUP:NO];
      }
     moved = NO;
     return YES;
    }
    
    
    -(void)animateViewToPosition:(UIView *)viewToMove directionUP:(BOOL)up {
    
       const int movementDistance = -60; // tweak as needed
       const float movementDuration = 0.3f; // tweak as needed
    
       int movement = (up ? movementDistance : -movementDistance);
       [UIView beginAnimations: @"animateTextField" context: nil];
       [UIView setAnimationBeginsFromCurrentState: YES];
       [UIView setAnimationDuration: movementDuration];
       viewToMove.frame = CGRectOffset(viewToMove.frame, 0, movement);
       [UIView commitAnimations];
    }
    

You can try this code

Add following code at top of your viewcontroller.m file

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
CGFloat animatedDistance;

Inside textFieldShouldBeginEditing

 -(BOOL) textFieldShouldBeginEditing:(UITextField*)textField
 {    
     CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator =  midline - viewRect.origin.y  - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
    * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;
    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }  
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
   if (orientation == UIInterfaceOrientationPortrait ||
    orientation == UIInterfaceOrientationPortraitUpsideDown)
   {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
   }
   else
   {
       animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
   }
   CGRect viewFrame = self.view.frame;
   viewFrame.origin.y -= animatedDistance;

   [UIView beginAnimations:nil context:NULL];
   [UIView setAnimationBeginsFromCurrentState:YES];
   [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
   [self.view setFrame:viewFrame];
   [UIView commitAnimations];
   return YES;

}

Inside textFieldShouldEndEditing

- (BOOL) textFieldShouldEndEditing:(UITextField*)textField
{
     CGRect viewFrame = self.view.frame;
     viewFrame.origin.y += animatedDistance;    
     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationBeginsFromCurrentState:YES];
     [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];    
     [self.view setFrame:viewFrame];
     [UIView commitAnimations];
}

You need to use TPAvoidingKeyBoard Library into your app your problem is solved..

https://github.com/michaeltyson/TPKeyboardAvoiding

See this link. Download this project and add it into your project.

Then first you have to add scrollview to your view and then all your textfield into that scrollview add that TPAvoidingScrollView To Custom Class.

Add Your TPAVoidingScrollviewClass here

Your code is good. but you have to change UIScrollView y position instead of height. I have change your code and update. please update this

step 1: In the ViewDidLoad method of your class set up to listen for messages about the keyboard:

// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

step 2: up/down your scrollview by implementing selector methods in same class

- (void)keyboardDidShow: (NSNotification *) notif{
    //Keyboard becomes visible
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
                                  scrollView.frame.origin.y - 220, 
                                  scrollView.frame.size.width,
                                  scrollView.frame.size.height);   //move up
}

- (void)keyboardDidHide: (NSNotification *) notif{
    //keyboard will hide
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
                                  scrollView.frame.origin.y + 220, 
                                  scrollView.frame.size.width,
                                  scrollView.frame.size.height);   //move down
}

Try this code

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
 {
     if(textField==tfLocation)
     {
         [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-50, self.view.frame.size.width, self.view.frame.size.height)];
     }
     return YES;
 }

 - (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.view setFrame:CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height)];
}
Related