简体   繁体   中英

Move UITextField above keyboard when using UIScrollView

I have a UIScrollView which contains a UIView container which contains multiple UITextField s.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM