Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move view when so that keyboard does not hide text field

In my app, when I click on a text field, the keyboard hides it. Please help me -- how can I move my view up when I click on the text field. I'm using this code in textFieldDidBeginEditing:

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

but it doesn't work.

like image 987
sandy Avatar asked Dec 09 '22 08:12

sandy


1 Answers

You should not trust textFieldDidBeginEditing: to adjust for the keyboard, since this method will be called even if the user is typing using a physical keyboard where an onscreen keyboard will not be displayed.

Instead listen to the UIKeyboardWillShowNotification, that is only triggered when the keyboard will actually be displayed. You need to do a three step process:

  1. Determine actual size of keyboard from the notifications userInfo dictionary. The size will differ from landscape/portrait, and different devices.
  2. Update the contentInset using the determined size. You can do it animated, the notification will even tell you the duration for the keyboard animation.
  3. Scroll the textfield into view, very easy to forget this!

You find more information and sample code from here

like image 97
PeyloW Avatar answered Dec 29 '22 01:12

PeyloW