Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add textfield and send button like typical chat app

I am struggling with something that seems like should be simple but is turning out to not be. I want to add a UITextField and send button anchored to the bottom of the screen above my tab bar and below my `UITableview'. I tried a footer, but it just adds to bottom of last row.

I need for it to move up when keyboard is presented.

like image 511
mreynol Avatar asked Jan 25 '14 19:01

mreynol


2 Answers

You can insert tableView in custom UIViewController and one more subview with send UITextField at the bottom. Then you follow notifications about keyboard presenting and change tableView and send UITextField frames as you wish. Something like that:

- (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];
}
like image 53
Wisors Avatar answered Oct 24 '22 00:10

Wisors


Swift:

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
}
like image 21
Esqarrouth Avatar answered Oct 24 '22 00:10

Esqarrouth