添加文本字段和发送按钮,就像典型的聊天应用

mre*_*nol 6 chat uitableview uitextfield ios

我正在努力寻找一些似乎应该简单的事情,但结果却不是.我想UITextField在我的标签栏上方和我的"UITableview"下方添加一个发送按钮并固定到屏幕底部.我尝试了一个页脚,但它只是添加到最后一行的底部.

我需要它在键盘出现时向上移动.

Wis*_*ors 5

您可以在自定义UIViewController中插入tableView,在底部使用send UITextField插入另一个子视图.然后,您按照有关键盘显示和更改tableView的通知,并根据需要发送UITextField帧.像这样的东西:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:(BOOL)animated];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillAppear:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillDisappear:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:(BOOL)animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Keyboard appearance/disappearance handling

- (void)keyboardWillAppear:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    [self.tableView setContentInset:contentInsets];
    [self.tableView setScrollIndicatorInsets:contentInsets];

    CGRect messageFrame = self.messageTextView.frame;
    messageFrame.origin.y -= keyboardSize.height;
    [self.messageTextView setFrame:messageFrame];
}

- (void)keyboardWillDisappear:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    [self.tableView setContentInset:UIEdgeInsetsZero];
    [UIView commitAnimations];
    [self.tableView setScrollIndicatorInsets:UIEdgeInsetsZero];

    CGRect messageFrame = self.messageTextView.frame;
    messageFrame.origin.y += keyboardSize.height;
    [self.messageTextView setFrame:messageFrame];
}
Run Code Online (Sandbox Code Playgroud)