如何将工具栏添加到UITableView obj-c / ios / xcode的底部

我如何添加Programmaticaly与uitextfield在桌面的底部工具栏? 像聊天或短信应用程序..预先感谢您..

首先创build一个视图来保存整个事情。 然后添加一个UITableview和UIToolbar以编程方式设置框架,使其出现在tableview下。将文本框添加到工具栏

UIView *placeholderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 400, 440)]; UITableView *tv=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 400, 400)]; [placeholderView addSubview:tv]; UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 400, 400, 40)]; [placeholderView addSubview:toolBar]; 

我只是find了一个更好的把戏!

  1. 确保没有导航栏(从失败的尝试)
  2. 拖放一个“Bar Button Item”(Xcode会神奇地将它放在底部)
  3. 注意 :如果您现在运行应用程序,您将不会看到任何东西! (所以请继续阅读)
  4. 在viewDidLoad下添加以下代码行:

self.navigationController.toolbarHidden = NO;

完成!

使用UIViewController子类而不是UITableViewController子类。

它应该是这样的:

 @interface ChatViewController : UIViewController @end #import "ChatViewController.h" @interface ChatViewController() <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UIToolbar *toolbar; @property (nonatomic, strong) UITextField *textField; @end @implementation ChatViewController -(UITableView *)tableView { if (!_tableView) { _tableView = [UITableView alloc] init]; CGRect frame = self.view.bounds; frame.size.height = frame.size.height - 44; _tableView.frame = frame; _tableView.delegate = self; _tableView.dataSource = self; } return _tableView; } -(UIToolbar *)toolbar { if (!_toolbar) { _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.tableView.frame.size.height,self.view.frame.size.width, 44)]; self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,_toolbar.frame.size.width -20)]; self.textField.delegate = self; UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:self.textField]; UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; // You'll need to add a button to send you text _toolbar.items = [NSArray arrayWithObjects:flexibleSpace, textFieldItem, flexibleSpace, nil]; } return _toolbar; } -(void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.tableView]; [self.view addSubview:self.toolbar]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideOrShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)viewDidUnload { [[NSNotificationCenter defaultCenter] removeObserver:self]; [super viewDidUnload]; } - (void)keyboardWillHideOrShow:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect keyboardFrameForToolbar = [self.toolbar.superview convertRect:keyboardFrame fromView:nil]; CGRect keyboardFrameForTableView = [self.tableView.superview convertRect:keyboardFrame fromView:nil]; CGRect newToolbarFrame = self.toolbar.frame; newToolbarFrame.origin.y = keyboardFrameForToolbar.origin.y - newToolbarFrame.size.height; CGRect newTableViewFrame = self.tableView.frame; newTableViewFrame.size.height = keyboardFrameForTableView.origin.y - newToolbarFrame.size.height; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{self.toolbar.frame = newToolbarFrame; self.tableView.frame =newTableViewFrame;} completion:nil]; } 

这将处理布置的意见和animation键盘外观。 您需要处理表视图和文本字段的委托和数据源方法。

这在某些方面不是很好,但我通过在另外一个View Controller上使用Container View来解决这个问题。

故事板截图

中级VC到Table VC segue是“Embed”

我用这个而不是“导航控制器上的工具栏”解决scheme,因为我将它添加到现有的〜15个屏幕的应用程序devise,我不知道如何在不同的屏幕上configuration不同的工具栏。