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

时间:2013-12-06 05:43:59

标签: ios objective-c cocoa-touch

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

view of form

当我开始编辑状态或以下字段时,键盘会隐藏该字段。像这样,

enter image description here

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

6 个答案:

答案 0 :(得分:3)

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

  1. 符合协议UITextFieldDelegate

  2. 有一个BOOL变量moved表示您的视图是否被移动。

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

答案 1 :(得分:3)

您可以尝试此代码

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

答案 2 :(得分:2)

您需要在应用中使用 TPAvoidingKeyBoard 库解决问题..

https://github.com/michaeltyson/TPKeyboardAvoiding

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

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

Add Your TPAVoidingScrollviewClass here

答案 3 :(得分:1)

你的代码很好。但您必须更改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
}

答案 4 :(得分:0)

试试此代码

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

答案 5 :(得分:0)

您会看到以下链接:

1)with scrollview

https://github.com/robbdimitrov/RDVKeyboardAvoiding

这对你有用。

2)with tableview

https://github.com/breeno/EditingUITableView