0

How do you move a UITextField on the screen so that it is not covered by the keyboard in iOS7, and then return it to its original spot inside the UIView once the keyboard is dismissed?

scriptocalypse
  • 4,832
  • 2
  • 26
  • 40
user3182143
  • 8,953
  • 3
  • 25
  • 34
  • 1
    possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – katzenhut Jan 20 '14 at 13:11
  • 1
    flagged it as duplicate. also, did you even google this before asking? – katzenhut Jan 20 '14 at 13:11
  • i need UItextfield scroll up on when user enter the first letter in the textfield – user3182143 Jan 20 '14 at 13:14

3 Answers3

1

You have to catch the notification for UIKeyboardWillShowNotification and edit your frames to move the UITextField to the right place (for example by using [UIView animateWithDuration:(NSTimeInterval) animations:^(void)animations]).

Here is a sample...

In init, add an observer on UIKeyboardWillShowNotification :

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

Then add the - keyboardWillShow method to update your view (supposing _textfield is the name of your UITextField) :

- (void)keyboardWillShow {
    NSLog(@"Show Keyboard");

    [UIView animateWithDuration:.2 animations:^{
        [_textField setFrame:_textField.frame];
        [_textField setFrame:CGRectMake(_textfield.frame.origin.x, _textfield.frame.origin.y - 240, _textfield.frame.size.width, _textfield.frame.size.height)];
    }];
}

You can use the UIKeyboardWillShowNotification notification to perform the inverse action.

adhumi
  • 475
  • 3
  • 15
1

in your Viewcontroller.h

@interface ViewController : UIViewController<UITextFieldDelegate>
{
 CGFloat animatedDistance;

 }

in ViewController.M //paste the below 4 line in anywhere on the class (outside the method)

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;

in delegate methods are

-(void)textFieldDidBeginEditing:(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;
}


animatedDistance = floor(PORTRAIT_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];
}

- (void)textFieldDidEndEditing:(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];

 }
Anbu.Karthik
  • 77,564
  • 21
  • 153
  • 132
  • 1
    Whoa! Some rather ancient animation methods. Given the question title asked about iOS 7, it's a shame you posted this - they're rather discouraged. – Luke Jan 20 '14 at 15:34
0

I recommend to use a UIScrollView to move your content up, when the keyboard is shown. Also Apple discourages us from using self defined constants for the keyboardsize: Other Languages may have different sizes. The correct size is embedded in the userInfo of the Notification:

Get the notifications for keyboard changes :

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

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

Remember to remove the observer like:

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

These methods handle the keyboard changes:

- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGRect kbRect = [self.view.window convertRect:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] fromView:self.scrollView];
    CGSize kbSize = kbRect.size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    NSTimeInterval duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;
    }];
}

Of course you could also manipulate the frame of the UITextView with these methods.

NoilPaw
  • 658
  • 5
  • 11
  • This is a very bad an complicated way to do it. – Gusutafu Nov 10 '14 at 14:01
  • Or maybe you answered a different question. Using a scrollview to move _contents_ is good, but putting an input field on a scroll view just to move it is not a good idea. Just use autolayout, or even change its position. – Gusutafu Nov 12 '14 at 09:30
  • As i said: "Of course you could also manipulate the frame of the UITextView with these methods." I just use the scrollview, whenever the content is larger than the space left after showing the keyboard. This way you are able to scroll to see all of the content. – NoilPaw Nov 14 '14 at 13:40