简体   繁体   English

使用UIScrollView时将UITextField移动到键盘上方

[英]Move UITextField above keyboard when using UIScrollView

I have a UIScrollView which contains a UIView container which contains multiple UITextField s. 我有一个UIScrollView ,其中包含一个UIView容器,其中包含多个UITextField

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. 我要执行以下操作:如果我选择了一个字段,并且该字段位于键盘下方,请在键盘上方上方加高文字字段,例如说20点。 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). 真正的问题是,当键盘处于活动状态时,最后一个字段根本不可见,因为我到达了UIView的末尾(在我的情况下,它的高度为836点)。

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. 我以为我可以提高整个视图的键盘高度,但这并不意味着当前字段将比键盘高20点。

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. 我已经有一个UIScrollView并且如果在完成对前一个字段的编辑后按键盘上的“下一个”,如果该字段位于键盘下方,则希望活动文本字段在键盘上方精确地20点。

Take a look at the KeyboardWillShow and KeyboardWillHide notifications. 看一下KeyboardWillShow和KeyboardWillHide通知。

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. 从那里,可以将UIView框架调整为所需的量,以使UITextFields位于键盘上方20个点。

Hope this helps. 希望这可以帮助。

try to use the TPKeyboardAvoiding library. 尝试使用TPKeyboardAvoiding库。 you have to just set the scrollView's class. 您只需要设置scrollView的类。 and it will handle all the things. 它会处理所有事情。

Hope it will Help You. 希望对您有帮助。

Add an observer for UIKeyboardWillShowNotification in your viewWillAppear like this - 像这样在您的viewWillAppearUIKeyboardWillShowNotification添加观察者-

    [[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 - 然后在keyboardWillShow:方法中,按照以下方式将滚动视图滚动到UITextField的框架:

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

If your textField is already on the screen, this won't do anything. 如果您的textField已经在屏幕上,则不会执行任何操作。 If the textField is below the keyboard, this would automatically move the scrollView to the textView's frame. 如果textField在键盘下方,这将自动将scrollView移至textView的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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