键盘隐藏了IOS中的文本字段

我在IOS中使用带有一些文本字段的scrollview创建了一个表单。 视图看起来像这样,

形式的看法

当我开始编辑或以下字段时,键盘会隐藏该字段。 喜欢这个,

在此处输入图像描述

我该怎么做才能看到以下字段(即状态和下方)?

您可以尝试此代码

在viewcontroller.m文件的顶部添加以下代码

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; 

在textFieldShouldBeginEditing中

  -(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; 

}

在textFieldShouldEndEditing中

 - (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]; } 

在发布问题之前,你一定要在堆栈溢出本身中搜索这个。 无论如何,你可以在这里找到答案。

  1. 符合协议UITextFieldDelegate

  2. 无论您的视图是否移动,都会移动BOOL变量以发出信号。

  3. 然后实现委托方法。

     -(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 Library,您的问题才能解决。

https://github.com/michaeltyson/TPKeyboardAvoiding

看到这个链接。 下载此项目并将其添加到您的项目中。

然后首先必须将scrollview添加到视图中,然后将所有文本字段添加到该滚动视图中,将TPAvoidingScrollView添加到自定义类。

在这里添加您的TPAVoidingScrollviewClass

你的代码很好。 但您必须更改UIScrollView y位置而不是高度。 我已更改您的代码并进行更新。 请更新这个

第1步:在您的类的ViewDidLoad方法中设置以侦听有关键盘的消息:

 // 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步:通过在同一个类中实现选择器方法来上/下滚动视图

 - (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 } 

试试这个代码

  - (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)]; } 

你看到这个链接:

1)使用scrollview

https://github.com/robbdimitrov/RDVKeyboardAvoiding

这对你有用。

2)与tableview

https://github.com/breeno/EditingUITableView