narsimelus narsimelus - 2 months ago 9x
Objective-C Question

Cannot move UIView onto UITableView

I'm creating a chat application in Objective-C. My conversation panel has two items, text field and table which will show messages. I'm using a UIView for text field and its button and this view is at the bottom of screen. Whenever I start typing, keyboard appears and as you can guess, text field is not visible.

I know using scroll views but this time, scroll view is not very suitable since I don't want table view to move up. Using a TableViewController instead of ViewController is an option for footer but in this option, if there's no message, footer goes top.

Thus, I'm using a ViewController and moving the view including text field programmatically. My code is:

- (void) keyboardWasShown:(NSNotification *)notification {

[UIView animateWithDuration:0.3 animations:^{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [keyboardFrameEnd CGRectValue];
[self.textFieldViewBottomConstraint setConstant:+(keyboardRect.size.height)];
[self.view setNeedsLayout];

This code works when there's no table view. I see my view goes up. However, as there is a table view, the view and text field is still staying invisible. They are under the table view.

How can I do this? How can I move a view onto table view?


Mostly, your textField containing view is going below tableView.

Try giving constraints as, tableview top to superview and the view top to tableview

and give other constraints. Take an outlet for top contraint of tableView. When you recieve keyboardWasShown change top constraint to some satisfying -ve value and when keyboard is dismissed again set the previous constraint for tableview top. I was previously working on this type of situation and this approach had worked for me, and might work for you.

or, if you really want to move the view above the tableView try using


it will avoid the view to go below tableview.