计算从键盘高度UITableview向上滚动多少

时间:2014-11-20 09:37:41

标签: ios objective-c uitableview uiscrollview uitextfield

我有这样的UIScrollView和UITextField。我知道如何在我的self.view中显示键盘高度。但是,UITextField位置不会固定在特定位置,因为用户可能会滚动。

如果只有键盘覆盖了uitextfield,我该怎么办?我需要使用CGPoint和convertPoint吗?我可以知道该怎么做吗?

enter image description here

3 个答案:

答案 0 :(得分:0)

您应该使用UITextFieldDelegate。 以下是如何实现它的示例:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
  if(!moved) {
   [self animateViewToPosition:self.view directionUP:YES];
  moved = YES; } 
}
 -(void)textFieldDidEndEditing:(UITextField *)textField {
  if(moved) {
  [self animateViewToPosition:self.view directionUP:NO];
   moved = NO;
    }
  [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 = -260; // 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 :(得分:0)

// Call this method somewhere in your view controller setup code.

- (void)registerForKeyboardNotifications

{

    [[NSNotificationCenter defaultCenter] addObserver:self

            selector:@selector(keyboardWasShown:)

            name:UIKeyboardDidShowNotification object:nil];



   [[NSNotificationCenter defaultCenter] addObserver:self

             selector:@selector(keyboardWillBeHidden:)

             name:UIKeyboardWillHideNotification object:nil];



}



// Called when the UIKeyboardDidShowNotification is sent.

- (void)keyboardWasShown:(NSNotification*)aNotification

{

    NSDictionary* info = [aNotification userInfo];

    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;



    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

    scrollView.contentInset = contentInsets;

    scrollView.scrollIndicatorInsets = contentInsets;



    // If active text field is hidden by keyboard, scroll it so it's visible

    // Your app might not need or want this behavior.

    CGRect aRect = self.view.frame;

    aRect.size.height -= kbSize.height;

    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {

        [self.scrollView scrollRectToVisible:activeField.frame animated:YES];

    }

}



// Called when the UIKeyboardWillHideNotification is sent

- (void)keyboardWillBeHidden:(NSNotification*)aNotification

{

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;

    scrollView.contentInset = contentInsets;

    scrollView.scrollIndicatorInsets = contentInsets;

}

答案 2 :(得分:0)

如果你有人想尝试它可能会有所帮助,但它可能不是最好的解决方案

   -(void)textFieldDidBeginEditing:(UITextField *)textField
 {
   CGRect actualframe = [textField convertRect:[textField bounds] toView:self.view];
   if ((actualframe.origin.y) >= (self.view.frame.size.height-keyboardheignt))
   {
      CGRect scrollfrm = [textField convertRect:[textField bounds] toView:tableview];
      [tableview setContentOffset:CGPointMake(0, scrollfrm.origin.y) animated:YES];//60
   }
 }

如果文本字段是表格的最后一行,则必须使用内容插入