2

I have a textField which is at bottom of the screen. When I begin editing, the keyboard appears and hides the textField.

Can anyone suggest how to solve this?

swiftBoy
  • 33,793
  • 26
  • 129
  • 124
Aadil
  • 714
  • 2
  • 12
  • 30
  • 1
    http://stackoverflow.com/questions/5636889/how-to-drag-my-view-a-little-bit-up-when-keyboard-comes-up/5637138#5637138 – HG's Jun 22 '11 at 07:21
  • Try this : http://stackoverflow.com/questions/703754/how-to-dismiss-keyboard-for-uitextview-with-return-key – Nitish Jun 22 '11 at 06:19
  • 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) – Oded Ben Dov Jan 14 '14 at 14:34
  • Try this short tutorial : https://www.youtube.com/watch?v=mUHUaj_wuPM It's probably not the best way to do it, but it did the trick for a test app. – n3wbie Feb 20 '17 at 13:22

5 Answers5

6

Generally you would put your content in a scroll view and move your scroll view up when the keyboard appears. There are many tutorials out there so google them. Here is one of them.

Deepak Danduprolu
  • 44,306
  • 11
  • 98
  • 105
6

You can use following code in two diffrent method, call it when editing began and end, to move your view up and down,(just change the valuew that is been subtracted in yourView.frame.origin.y-100 to adjust according to your need)

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.5];      
    yourView.frame = CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y-100, yourView.frame.size.width, yourView.frame.size.height);

    [UIView commitAnimations];
rptwsthi
  • 9,855
  • 10
  • 65
  • 102
4

See this code snippet. And use one UIButton to dismiss UIKeyboard.

- (void) animateTextField: (UITextField *)textField up:(BOOL) up
{
const int movementDistance = 200;
const float movementDuration = 0.4f;

int movement = (up ? -movementDistance : movementDistance);

up ? (self.button.enabled= YES) : (self.button.enabled = NO);

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}

-(void)textFieldDidBeginEditing:(UITextField*) inTextField
{
[self animateTextField:mText up:YES];
[self addObservers];
}

- (void)addObservers 
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:@"UITextFieldTextDidChangeNotification" object:nil];    
}

- (void)textFieldDidChange:(NSNotification*)aNotification 
{
   NSLog(@"TextField data=%@",mText.text);
}   

- (IBAction) doneBtn:(id)sender
{
[mText resignFirstResponder ];
[self animateTextField:mText up:NO];
}
Naveen Thunga
  • 3,667
  • 2
  • 22
  • 31
2

When you tap on textField keyboard up then your textField hide by it. so waht you need, you need to make your view up not keyboardresign is a solution. so for making you view up use

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField  delegate for the logic. now see the code

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    if(textField.tag==1)  //add tag to your textField to identify it
    {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];

        CGRect viewFrame=self.view.frame;
        viewFrame=sinViewFrame.size.height+100;
        viewFrameorigin.y-=100; // as required
        self.view.frame=viewFrame;
        [UIView commitAnimations];

   }

}

hope it helps you.

Ishu
  • 12,806
  • 5
  • 31
  • 51
0

I use auto layout constraint to solve this problem. I hold constraint as a property, and play with it when keyboard appears / disappears. I simply move views above the textfield out of screen, and textfield to the top. After finishing edit, I move them by the constraint to their original position.

-(void)keyboardWillShow:(NSNotification*)notification{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
    self.topSpaceLayoutConstraint.constant = -150;
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
} completion:NULL];
}

-(void)keyboardWillHide:(NSNotification*)notification
{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
    self.topSpaceLayoutConstraint.constant = 10;
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
} completion:NULL];
}

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpaceLayoutConstraint;
Tom
  • 3,349
  • 20
  • 73
  • 131