keyboard is hiding the text field in IOS
本问题已经有最佳答案,请猛点这里访问。
我在iOS中使用滚动视图创建了一个带有一些文本字段的表单。这里的景色是这样的,
当我开始编辑状态或下面的字段时,键盘会隐藏该字段。这样地,
我应该怎么做才能看到下面的字段(即状态和下面的字段)??
你可以试试这个代码
在viewcontroller.m文件的顶部添加以下代码
1 2 3 4 5 6 | static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162; CGFloat animatedDistance; |
应在文本字段内注册
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | -(BOOL) textFieldShouldBeginEditing:(UITextField*)textField { CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height; CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; CGFloat heightFraction = numerator / denominator; if (heightFraction < 0.0) { heightFraction = 0.0; } else if (heightFraction > 1.0) { heightFraction = 1.0; } UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); } else { animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); } CGRect viewFrame = self.view.frame; viewFrame.origin.y -= animatedDistance; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; return YES; |
}
内部文本字段应专用
1 2 3 4 5 6 7 8 9 10 | - (BOOL) textFieldShouldEndEditing:(UITextField*)textField { CGRect viewFrame = self.view.frame; viewFrame.origin.y += animatedDistance; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; } |
在发布问题之前,您一定要在堆栈溢出本身中搜索这个。无论如何,你可以在这里找到答案。
符合
有一个bool变量
然后实现委托方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | -(void)textFieldDidBeginEditing:(UITextField *)textField { if(!moved) { [self animateViewToPosition:self.view directionUP:YES]; moved = YES; } } -(void)textFieldDidEndEditing:(UITextField *)textField { [textField resignFirstResponder]; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; if(moved) { [self animateViewToPosition:self.view directionUP:NO]; } moved = NO; return YES; } -(void)animateViewToPosition:(UIView *)viewToMove directionUP:(BOOL)up { const int movementDistance = -60; // tweak as needed const float movementDuration = 0.3f; // tweak as needed int movement = (up ? movementDistance : -movementDistance); [UIView beginAnimations: @"animateTextField" context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration]; viewToMove.frame = CGRectOffset(viewToMove.frame, 0, movement); [UIView commitAnimations]; } |
你需要在你的应用程序中使用tpavoidingkeyboard库,你的问题就解决了。
https://github.com/michaeltyson/tpkeyboardavailing
请参见此链接。下载此项目并将其添加到您的项目中。
然后,首先必须将ScrollView添加到视图中,然后将所有文本字段添加到该ScrollView中,将该TpavoidingScrollView添加到自定义类中。
你的代码很好。但是你必须改变uiscrollview的y位置而不是高度。我已经更改了你的代码并更新了。请更新这个
步骤1:在类的VIEWDIDLOAD方法中,设置为侦听有关键盘的消息:
1 2 3 4 5 6 7 8 9 10 | // Listen for keyboard appearances and disappearances [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; |
步骤2:通过在同一个类中实现选择器方法来向上/向下滚动视图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | - (void)keyboardDidShow: (NSNotification *) notif{ //Keyboard becomes visible scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y - 220, scrollView.frame.size.width, scrollView.frame.size.height); //move up } - (void)keyboardDidHide: (NSNotification *) notif{ //keyboard will hide scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y + 220, scrollView.frame.size.width, scrollView.frame.size.height); //move down } |
您可以看到此链接:
1)使用滚动视图
https://github.com/robbdimitrov/rdvkeyboardavointing/避开
这对你很有用。
2)带TableView
https://github.com/breeno/editinguitableview网站
试试这个代码
1 2 3 4 5 6 7 8 9 10 11 12 13 | - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if(textField==tfLocation) { [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-50, self.view.frame.size.width, self.view.frame.size.height)]; } return YES; } - (void)textFieldDidEndEditing:(UITextField *)textField { [self.view setFrame:CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height)]; } |