如何在 UITableView obj-c/ios/xcode 的底部添加工具栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12687014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a toolbar to the BOTTOM of a UITableView obj-c/ios/xcode
提问by snksnk
how do I add Programmaticaly a toolbar with uitextfield at the bottom of a tableview? Like a chat or sms app.. thank you in advance..
如何在表格视图底部以编程方式添加带有 uitextfield 的工具栏?像聊天或短信应用程序.. 在此先感谢您..
采纳答案by Lithu T.V
First create a view to hold the whole thing. Then add a UITableview and UIToolbar programmatically set frame so that it appears under the tableview .Add the textfield to the toolbar
首先创建一个视图来保存整个事情。然后添加一个 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];
回答by alejandrormz
I just found a better trick!
我刚刚发现了一个更好的技巧!
- Make sure there is NO Navigation Bar (from your failed attempts)
- Drag and drop a "Bar Button Item" (Xcode will magically place it for you at the bottom)
- NOTE: If you Run the App now you won't see anything! (so keep reading)
- Add the following line of code under viewDidLoad:
- 确保没有导航栏(来自您失败的尝试)
- 拖放一个“Bar Button Item”(Xcode 会神奇地把它放在底部)
- 注意:如果您现在运行应用程序,您将看不到任何内容!(所以继续阅读)
- 在 viewDidLoad 下添加以下代码行:
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbarHidden = NO;
Done!
完毕!
回答by Moxy
Use a UIViewController subclass instead of UITableViewController subclass.
使用 UIViewController 子类而不是 UITableViewController 子类。
it should be something like this :
它应该是这样的:
@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];
}
This would handle laying out the views and animating keyboard appearance. You'll need to handle delegate and datasource methods for the table view and the text field.
这将处理布局视图和动画键盘外观。您需要处理表格视图和文本字段的委托和数据源方法。
回答by Brian Broom
It's not great in some ways, but I solved this by using a Container View on an additional View Controller.
在某些方面它不是很好,但我通过在附加的视图控制器上使用容器视图解决了这个问题。
The Intermediate VC to Table VC segue is "Embed"
中级 VC 到 Table VC segue 是“嵌入”
I used this instead of the "Toolbar on Nav Controller" solution because I'm adding this to an existing app design with ~15 screens, and I wasn't sure how to configure different toolbars on different screens.
我使用它而不是“导航控制器上的工具栏”解决方案,因为我将它添加到具有约 15 个屏幕的现有应用程序设计中,并且我不确定如何在不同的屏幕上配置不同的工具栏。


