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

时间:2014-01-25 19:28:21

标签: ios uitableview uitextfield chat

我正在努力寻找一些看起来应该很简单的事情,但事实证明并非如此。我想在我的标签栏上方和“UITableview”下方添加一个UITextField和发送按钮。我尝试了一个页脚,但它只是添加到最后一行的底部。

当键盘出现时我需要向上移动。

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:2)

夫特:

var commentField:UITableViewCell!


override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(true)
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillAppear(notification: NSNotification){

    var userInfo:NSDictionary = notification.userInfo!
    var keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size

    var contentInsets:UIEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)

    self.tableView.contentInset = contentInsets
    self.tableView.scrollIndicatorInsets = contentInsets

    var messageFrame:CGRect = self.commentField.frame
    messageFrame.origin.y -= keyboardSize.height
    self.commentField.frame = messageFrame

}

func keyboardWillDisappear(notification: NSNotification){
    var userInfo:NSDictionary = notification.userInfo!
    var keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.25)
    self.tableView.contentInset = UIEdgeInsetsZero
    UIView.commitAnimations()

    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero

    var messageFrame:CGRect = self.commentField.frame
    messageFrame.origin.y += keyboardSize.height
    self.commentField.frame = messageFrame
}