1

我正在进行与以下功能类似的聊天:

在此处输入图像描述

当用户单击消息气泡时,我需要它来提升 UITextField 和 UITableView (当然是文本框上方的表格视图)。同样,当他们发送或收回消息时,它应该回到原来的状态。

我尝试了此处发布的解决方案

两个通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object:nil];

和实际功能:

- (void)keyboardWillHideOrShow:(NSNotification *)note
{
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil];

    CGRect newTextFieldFrame = self.myTextField.frame;
    newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height;

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
        self.myTextField.frame = newTextFieldFrame;
    } completion:nil];
}

但它有两个问题:

  1. 我不知道如何提升键盘顶部的 tableview
  2. 当键盘回落时,输入框完全消失。
4

1 回答 1

2

这可能会有所帮助....不能归功于它,但对于我的生活,我不记得它来自哪里...虽然曾经帮助我完成了一个项目,所以这就是我使用它的方式。

当基于 BOOL 值 YES 或 NO 调用/关闭键盘时,这将上下移动视图。这种方式还允许您对它的其他方面有更多的控制。

- (void) animateTextField: (UITextField*) textField up: (BOOL)up
{
const int movementDistance = 100;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}

然后实现它:

-(IBAction)goingup:(id)sender
{
   //Bounces the text field up for editing
   [self animateTextField: yourtextfield up: YES];} 
}


-(IBAction)backdown:(id)sender
{
   //Bounces it back when keyboard is dismissed 
   [self animateTextField: yourtextfield up: NO];   
}

将操作连接到您的文本字段 Editing did begin 和 Editing did end outlets 并且您已设置。

希望有帮助。

于 2013-01-12T03:07:27.060 回答