如何在表格视图底部以编程方式添加带有 uitextfield 的工具栏?喜欢聊天或短信应用程序..提前谢谢你..
问问题
20633 次
4 回答
30
我刚刚发现了一个更好的技巧!
- 确保没有导航栏(来自您的失败尝试)
- 拖放一个“Bar Button Item”(Xcode 会神奇地将它放置在底部)
- 注意:如果您现在运行应用程序,您将看不到任何东西!(所以请继续阅读)
- 在 viewDidLoad 下添加以下代码行:
self.navigationController.toolbarHidden = NO;
完毕!
于 2013-11-15T16:56:57.913 回答
5
使用 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];
}
这将处理布局视图和动画键盘外观。您需要处理表视图和文本字段的委托和数据源方法。
于 2012-10-02T09:23:00.887 回答
2
首先创建一个视图来容纳整个事物。然后添加一个 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];
于 2012-10-02T09:09:06.133 回答