Moving a view when the keyboard does not hide the text field

In my application, when I click on a text field, the keyboard hides it. Please help me - how can I move my view when I click on the text box. I use this code in textFieldDidBeginEditing:

 self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0); self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0); 

but that will not work.

+4
source share
2 answers

You can do the following, but first make sure you set the UITextField delegate for yourself and

 #define kOFFSET_FOR_KEYBOARD 350; 

up. This is how far you want to move the view.

 //method to move the view up/down whenever the keyboard is shown/dismissed -(void)setViewMovedUp:(BOOL)movedUp { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; // if you want to slide up the view [UIView setAnimationBeginsFromCurrentState:YES]; CGRect rect = self.view.frame; if (movedUp) { // 1. move the view origin up so that the text field that will be hidden come above the keyboard // 2. increase the size of the view so that the area behind the keyboard is covered up. if (rect.origin.y == 0 ) { rect.origin.y -= kOFFSET_FOR_KEYBOARD; //rect.size.height += kOFFSET_FOR_KEYBOARD; } } else { if (stayup == NO) { rect.origin.y += kOFFSET_FOR_KEYBOARD; //rect.size.height -= kOFFSET_FOR_KEYBOARD; } } self.view.frame = rect; [UIView commitAnimations]; } - (void)keyboardWillHide:(NSNotification *)notif { [self setViewMovedUp:NO]; } - (void)keyboardWillShow:(NSNotification *)notif{ [self setViewMovedUp:YES]; } - (void)textFieldDidBeginEditing:(UITextField *)textField { stayup = YES; [self setViewMovedUp:YES]; } - (void)textFieldDidEndEditing:(UITextField *)textField { stayup = NO; [self setViewMovedUp:NO]; } - (void)viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:self.view.window]; } - (void)viewWillDisappear:(BOOL)animated { // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } 
+1
source

You can not trust textFieldDidBeginEditing: to configure the keyboard, since this method will be called even if the user types using the physical keyboard, where the on-screen keyboard will not be displayed.

Instead, listen to the UIKeyboardWillShowNotification , which only starts when the keyboard is displayed. You need to complete the three-step process:

  • Determine the actual size of the keyboard from userInfo dictionaries. The size will differ from landscape / portrait and different devices.
  • Update the contentInset with a specific size. You can make it animated, the notification will even tell you the duration of the keyboard animation.
  • Scroll through the text field in the field of view, it is very easy to forget about it!

You will find more information and sample code from here.

+13
source

All Articles