键盘正在IOS中隐藏文本字段
我已经在IOS中使用带有一些文本字段的滚动视图创建了一个表单。景色是这样的, 当我开始编辑状态或以下字段时,键盘会隐藏该字段。像这样,键盘正在IOS中隐藏文本字段,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我已经在IOS中使用带有一些文本字段的滚动视图创建了一个表单。景色是这样的, 当我开始编辑状态或以下字段时,键盘会隐藏该字段。像这样, 我应该怎么做才能看到以下字段(即状态和以下)?您需要在应用程序中使用TPAvoidingKeyBoard库您的问题已经解决 请参阅此链接。下载此项目并将其添加到您的项目中 然后,首先必须将scrollview添加到视图中,然后将所有文本字段添加到该scrollview中,并将TPAvoidingScrollView添加到自定义类中 试试这段代码 - (B
我应该怎么做才能看到以下字段(即状态和以下)?您需要在应用程序中使用TPAvoidingKeyBoard库您的问题已经解决 请参阅此链接。下载此项目并将其添加到您的项目中 然后,首先必须将scrollview添加到视图中,然后将所有文本字段添加到该scrollview中,并将TPAvoidingScrollView添加到自定义类中 试试这段代码
- (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)];
}
在发布问题之前,您肯定应该在堆栈溢出本身中搜索此项。无论如何,你可以在这里找到答案
UITextFieldDelegate
移动
以发出信号-(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];
}
你的代码很好。但是,您必须更改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)];
}
在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;
内部文本字段应开始编辑
-(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;
在发布到SO之前,至少先到谷歌搜索一下。@Virussmca:浏览了6153次(2015年11月1日)。我把他们(搜索谷歌的人)带到这里:据我所知,DNo在iOS8中不再工作(在iOS10上)我尝试过使用这种方法,但是键盘消失了(完全是黑色的)。有人有过类似的问题吗?@pita是的,发生在我身上,但在endEditing方法中它并不是真正的问题。键盘也是黑色的,画面越来越大。在我点击文本字段时,是否可以将此代码与UIViewController一起使用?因为我的视图没有滚动视图,问题是只有文本字段被翻译,而不是所有的主视图。同样的谢谢,有没有办法为运动设置动画?@Chris你可以将此代码放入
[UIView animateWithDuration:animations:completion:
中进行动画设置