0

I have an UIViewController containing textFields. The problem is that when I created the UIViewController I didn't use the scroll view. So when I use the code below to translate the view when the keyboard is opened, only the view1 that contains the textField is translated. The view1 is in the main view and it has as contraints: fixed width and height, a vertical bottom spacing and a center X with the with the super view.

Is i possible to translate the parent root view in order to show a textfield when the keyboard is showed without using the scrollView?

This is the code that I use to show the textField (the code is taken from here)

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;
}

- (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];
}
Community
  • 1
  • 1
Ne AS
  • 1,490
  • 3
  • 26
  • 58
  • Why do you have all of those keyboard constants defined as macros? Get them at runtime using the appropriate keyboard notifications. – rmaddy Jan 05 '17 at 23:34
  • Can you give me an example please? And is it possible to get it work without scrollView? – Ne AS Jan 05 '17 at 23:36
  • Search on [UIKeyboardWillShowNotification UIKeyboardFrameEndUserInfoKey UIKeyboardAnimationDurationUserInfoKey](http://stackoverflow.com/search?q=%5Bobjective-c%5D+UIKeyboardWillShowNotification+UIKeyboardFrameEndUserInfoKey+UIKeyboardAnimationDurationUserInfoKey) – rmaddy Jan 05 '17 at 23:41
  • okay I will. Thanks – Ne AS Jan 05 '17 at 23:46
  • http://www.apeth.com/iOSBook/ch23.html#_keyboard_covers_text_field – matt Jan 06 '17 at 05:46

1 Answers1

0

You can try to work with the code below. You will have to adjust the code to take into consideration if your view extend below or above the keyboard.

- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSLog(@"Keyboard showing");

    _keyboardSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect frame1 = _myScrollView.frame;
    float object1Y = _myScrollView.frame.size.height - _keyboardSize.height;
    frame1.origin.y = frame1.origin.y - object1Y;

    [UIView animateWithDuration:0.3f animations:^{
        _myScrollView.frame = frame1;
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    NSLog(@"Keyboard hiding");

    _keyboardSize = CGSizeMake(0, 0);
    [UIView animateWithDuration:0.3f animations:^{
        // Reset the scroll view to the original position

    } completion:^(BOOL finished){

    }];
}
Alex
  • 995
  • 12
  • 25
  • Thanks. It will work with the view (without scrollView)? – Ne AS Jan 06 '17 at 09:20
  • It should. The animation changes the view frame coordinates. Please note that the code is not complete. I don't know your specific case, so you will have to adapt it to your situation. – Alex Jan 06 '17 at 17:03