0

I have multiple UITextFields and i need to move view up when keyboard appears. But I need to it only for 3 of bottom fields and doesn't want to move view for other field on top. I use this code, but its move view when user tap on every field, instead i need move view only when user tap on 2 bottom fields

- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification 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];
}

#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect f = self.view.frame;
        f.origin.y = -115.0f;  //set the -35.0f to your required value
        self.view.frame = f;
    }];
}

-(void)keyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect f = self.view.frame;
        f.origin.y = 0.0f;
        self.view.frame = f;
    }];
}
Steve Sahayadarlin
  • 1,164
  • 3
  • 15
  • 32
  • 2
    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) – nielsbot Jul 23 '14 at 06:41
  • this is the old method but working fine for me try this http://stackoverflow.com/questions/24181034/textfields-is-hiding-by-the-keypad/24181352#24181352 – Anbu.Karthik Jul 23 '14 at 06:43
  • Use `EKKeyboardAvoiding`Library available in github here https://github.com/kirpichenko/EKKeyboardAvoiding. – Rugmangathan Jul 23 '14 at 06:45

3 Answers3

5

You can change the frame of any UIViews,UIButton.... inside UITextfield delegate method textFieldDidBeginEditing:This delegate will call when you tap on the textfield

 - (void)textFieldDidBeginEditing:(UITextField *)textField{ 

      //here you will get the selected textfield
      //check whether the textfield is equal to any of three bottom field
       if(textfield==BottomTextfield)
       {
            //here you can change the frame of the UIViews
       }
   }
Sefi
  • 216
  • 2
  • 7
0

You need to change the UIView's frame using the UITextField's delegate methods:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    int tag = textField.tag;
    int section = tag / 1000;
    tag -= 1000 * section;
    section -= 1;

    CGPoint point = [[textField superview] convertPoint:textField.center toView:self.view];


    CGPoint contentOffset = tableView.contentOffset;

    if (IOS_7) {
        contentOffset.y += (point.y - 230);
        // Adjust this value as you need

    }
    else{
        contentOffset.y += (point.y - 150); // Adjust this value as you need

    }
    currentY = contentOffset.y;
    [tableView setContentOffset:contentOffset animated:YES];

}

- (void)textFieldDidEndEditing:(UITextField *)textField {
   [tableView setContentOffset:CGPointMake(0, 0)];
}

Hope this helps...!!!

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
0

You can check first responder (active text field). This SO answer shows how to do it: https://stackoverflow.com/a/1823360/2486394 . And move view only for some text fields.

Community
  • 1
  • 1
Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43