0

I would like to add a textfield and send button that sticks to the bottom of uitableview similar to a chat app. I have come across comments on embedding a UITableView as a container view within a UIViewController.

However, they seem to lack an example on how to achieve this. More specifically details including where to add a textfield/button and move textfield up when keyboard appears, etc. Thanks!

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
mir
  • 183
  • 1
  • 12
  • Refer this recent post on [Floating button on `UITableView`.](http://stackoverflow.com/questions/38887389/a-floating-button-fixed-at-the-bottom-of-uitableview-with-scrollviewdidscroll-no/38887755#38887755) It might be helpful. Its using `UITableViewController` instead `UIViewController` though it is suitable in your requirement. – Dipen Panchasara Aug 17 '16 at 06:11

2 Answers2

5

follow the steps using the storyboard

1) drag a uiviewcontroller from the object library.
2) drag and drop the textfield and button and place it at the position you want
3) drag and drop a container view.
4) delete the default uiviewcontroller comes with the container view
5) drag a uitableviewcontroller and make a segue and the segue should be embedsegue.

and for keyboard handling you can go with IQKeyboardManager library https://github.com/hackiftekhar/IQKeyboardManager

Anil Kumar
  • 945
  • 7
  • 16
1

A very simple chat model code, you can take a look:

#import "ViewController.h"

@interface ViewController ()

@property UIView* containerView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

_containerView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-30)];
UITextField* tfInput = [[UITextField alloc] initWithFrame:CGRectMake(0, tableView.frame.size.height, [UIScreen mainScreen].bounds.size.width-50, 30)];
tfInput.backgroundColor = [UIColor grayColor];
UIButton* btnSend = [[UIButton alloc] initWithFrame:CGRectMake(tfInput.frame.size.width, tfInput.frame.origin.y, 50, 30)];
[btnSend setTitle:@"Send" forState:UIControlStateNormal];
[btnSend setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btnSend addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];

[_containerView addSubview:tableView];
[_containerView addSubview:tfInput];
[_containerView addSubview:btnSend];
[self.view addSubview:_containerView];

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

- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
float keyboardHeight = [aValue CGRectValue].size.height;
//Resize the container
_containerView.frame = CGRectMake(0,  - keyboardHeight, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}

-(void)btnClicked{
[self.view endEditing:YES];
}

- (void)keyboardWillHide:(NSNotification *)notification {
_containerView.frame = [UIScreen mainScreen].bounds;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

This is a screenshot for using storyboard:

enter image description here

Alanc Liu
  • 1,294
  • 10
  • 15
  • Thank you! Is there a way to make it work for a tableview embedded as a container view in viewcontroller via storyboard (not created programmatically like the above example)? – mir Aug 10 '16 at 17:12
  • It's the same as programmatically, just drag a UIView as container, then drag tableview, textfield, button in container one by one, finally link them to ViewContoller.h as an outlet(for button you can just use an action). About the keyboard event, you must use code to finish it, like above-mentioned. – Alanc Liu Aug 11 '16 at 02:36
  • By the way, I suggest you use the code to create your app, especially for the complicated UI, or you will get so many problems to finish it. – Alanc Liu Aug 11 '16 at 02:43