0

I have a UIScrollView which contains a UIView container which contains multiple UITextFields.

I want to do the following: If I select a field and it's below the keyboard, raise raise text field say 20 points above the keyboard. If it's above the keyboard then don't do anything.

The real problem is that the last field is not visible at all when the keyboard is active because I reached the end of the UIView (in my case it's 836 points tall).

I thought that I could raise the entire view the height of the keyboard, but it doesn't mean that the current field will be EXACTLY 20 points above the keyboard.

Any ideas?

EDIT: This is not a duplicate question. I already have a UIScrollView and I want the active text field to be EXACTLY 20 points above the keyboard IF the field is below the keyboard when pressing "Next" on the keyboard after finishing editing the previous field.

Lawrence413
  • 1,784
  • 1
  • 12
  • 23
  • Change the scroll view's `contentInset` so that the whole scroll view is scrollable within the area above the keyboard. – matt Jun 07 '16 at 15:38
  • Additionally, you can use `setContentOffset(_:, animated:)` to achieve the 20 point margin above the keyboard. Use keyboard notifications `UIKeyboardWillShowNotification` and `UIKeyboardWillHideNotification`) to get the size of the keyboard when it appears. – paulvs Jun 07 '16 at 15:48
  • @paulvs I haven't worked with these before so a little code would be helpful. – Lawrence413 Jun 07 '16 at 15:50

3 Answers3

1

Take a look at the KeyboardWillShow and KeyboardWillHide notifications.

When implemented, the height of the keyboard can be retrieved from the notification object parameter.

From there, your UIView frame can be adjusted by the desired amount so that the UITextFields are above the keyboard by 20 points.

Hope this helps.

cmart
  • 211
  • 2
  • 4
  • I understand that, and what @matt said, but this only fixes the last field not being visible. The problem is moving the entire view relative to the active field which will be 20 points above the keyboard. I can't manually move the view X points. It has to adapt. – Lawrence413 Jun 07 '16 at 15:46
  • Thanks! I found an apple tutorial by starting from what you said and it fixed my problem. – Lawrence413 Jun 09 '16 at 12:42
  • @Lawrence413 No problem mate! – cmart Jun 10 '16 at 20:00
0

try to use the TPKeyboardAvoiding library. you have to just set the scrollView's class. and it will handle all the things.

Hope it will Help You.

JigneshP
  • 328
  • 2
  • 7
0

Add an observer for UIKeyboardWillShowNotification in your viewWillAppear like this -

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

And then in the keyboardWillShow: method, scroll your scroll view to the frame of the UITextField in the following manner -

- (void) keyboardWillShow:(UIView *)aView {
[scrollview scrollRectToVisible:textView.frame animated:YES];
}

If your textField is already on the screen, this won't do anything. If the textField is below the keyboard, this would automatically move the scrollView to the textView's frame.

genaks
  • 737
  • 1
  • 10
  • 23