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

6

我应该如何在表格视图底部编程添加一个带有UITextField的工具栏?就像聊天或短信应用程序一样。谢谢。


將以下與編程相關的內容從英文翻譯成中文。僅返回翻譯後的文本:還沒有任何東西…我看到的所有帖子都是關於添加它在頂部。 - snksnk
4个回答

31

我发现了一个更好的技巧!

  1. 确保没有导航栏(从您之前的尝试中删除)
  2. 拖放一个“Bar Button Item”(Xcode将自动将其放置在底部)
  3. 注意:如果现在运行应用程序,您将看不到任何内容! (因此请继续阅读)
  4. 在viewDidLoad下面添加以下代码行:

self.navigationController.toolbarHidden = NO;

完成!


这正是我正在寻找的! - Gabriel Cebrian
你太棒了!非常感谢。我原本有它,然后我把它移除了,现在我忘记了怎么放回去了,哈哈。 - pqsk

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];  
}

这将处理视图的布局和键盘出现时的动画效果。您需要处理表格视图和文本字段的委托和数据源方法。


好的回答,看起来很有前途,但我想只要表格视图和工具栏的尺寸设置正确,使用 UITableViewController 没有任何缺点。我想知道为什么你建议不要使用 UITableViewController - Ömer Faruk Almalı
如果您尝试向 UITableViewController 的主视图(即表格视图)添加子视图,则可能会遇到麻烦。 - Moxy

2

首先创建一个视图来容纳整个内容。 然后通过编程方式添加UITableview和UIToolbar并设置其框架,以便它出现在tableview下方。将textfield添加到工具栏中。

    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]; 

1

在某些方面它并不完美,但我通过在另一个视图控制器上使用容器视图来解决了这个问题。

Storyboard screenshot

中间VC到表VC的转场是“嵌入”。 我使用了这个而不是“导航控制器上的工具栏”解决方案,因为我正在将它添加到一个已有的设计中,其中包含大约15个屏幕,并且我不确定如何在不同的屏幕上配置不同的工具栏。

我同意这并不是很好,但它只是绕过了一个明显的问题,即在IOS中,如果您不先将UIVC嵌入UINavigationController,则无法将UIToolbar添加到UIViewController。在我的情况下,由于UINC的其他“设计特性”,我无法将我的UIVC嵌入UINC中。 - Mark Lummus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接