7

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)??

Muhammed Aslam C
  • 769
  • 1
  • 7
  • 14

5 Answers5

4

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];
    }
    
Balram Tiwari
  • 5,537
  • 2
  • 21
  • 40
4

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];
}
Thomas Leduc
  • 1,022
  • 1
  • 17
  • 44
megha
  • 84
  • 5
  • I tried using this method, however the keyboard disappears(its completely black). anyone had similar issues? – Pita Mar 09 '15 at 17:58
  • @pita yes, happening with me and it doesn't go in the real position in endEditing method. Keyboard is also black and frame is increasing up and up agin i click on the textfield – Anurag Sharma Aug 26 '16 at 11:16
  • Is it possible to use this code with an UIViewController? Because my view haven't a scroll view and the problem is that only the text field is translated and not all the main view. – Ne AS Jan 05 '17 at 18:18
  • Same https://stackoverflow.com/a/26237038/5790492 – Nik Kov Apr 02 '18 at 17:02
2

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

Jitendra
  • 4,944
  • 2
  • 20
  • 42
1

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
}
Community
  • 1
  • 1
Haresh Ghatala
  • 1,650
  • 14
  • 23
0

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)];
}
Pradhyuman sinh
  • 3,938
  • 1
  • 19
  • 38