[英]Move UITextField above keyboard when user click on textfield
I want to move UITextField
up above keyboard when user clicks UITextField
. 当用户单击
UITextField
时,我想将UITextField
向上移动到键盘上方。
I searched about this and some suggestion are to move whole view up and some suggestion are add scrollview. 我搜索了一下,一些建议是将整个视图向上移动,有些建议是添加scrollview。
But I don't want to use scrollview. 但是我不想使用scrollview。
How can I do this? 我怎样才能做到这一点?
Give your UITextField
bottom Constant and when you found keyboard height in keyboardWillShow
increase your UITextField
bottom constraint to KeyboardHeight
. 给你
UITextField
底部常数,当你发现在键盘高度keyboardWillShow
增加你UITextField
底部约束KeyboardHeight
。
Just Put below code in ViewWillAppear. 只需将下面的代码放在ViewWillAppear中。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
And put below code in ViewWillDisappear. 并将下面的代码放在ViewWillDisappear中。
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
Method 方法
- (void)keyboardWillShow:(NSNotification *)notification
{
// Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
int keyboardHeight = MIN(keyboardSize.height,keyboardSize.width);
textBottom.constant = 0.0f;
for (UIView *vw in commentView.subviews) {
if ([vw isKindOfClass:[UITextField class]]) {
txtComment = (UITextField *)vw;
textBottom.constant = keyboardHeight;
}
}
}
Here is one awesome solution: https://github.com/hackiftekhar/IQKeyboardManager 这是一个很棒的解决方案: https : //github.com/hackiftekhar/IQKeyboardManager
It easy to integrate and effortless. 它易于集成且毫不费力。
You won't have to use a UIScrollView in each of your UIViewControllers. 您不必在每个UIViewControllers中都使用UIScrollView。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.