【问题标题】:How to add a toolbar to the BOTTOM of a UITableView obj-c/ios/xcode如何在 UITableView obj-c/ios/xcode 的底部添加工具栏
【发布时间】:2012-10-02 08:39:07
【问题描述】:

如何以编程方式在表格视图底部添加带有 uitextfield 的工具栏?喜欢聊天或短信应用程序..提前谢谢你..

【问题讨论】:

  • 还没有.. 我看到的任何帖子都是关于将其添加到顶部

标签: objective-c ios xcode uitableview toolbar


【解决方案1】:

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

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

self.navigationController.toolbarHidden = NO;

完成!

【讨论】:

  • 这正是我想要的!
  • 你成功了!太感谢了。原来有,后来删了,忘记怎么放回去了哈哈。
【解决方案2】:

使用 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
  • UITableViewController 如果您尝试将子视图添加到表视图的主视图中,可能会很棘手。
【解决方案3】:

首先创建一个视图来容纳整个事物。 然后添加一个 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]; 

【讨论】:

    【解决方案4】:

    在某些方面它不是很好,但我通过在额外的视图控制器上使用容器视图解决了这个问题。

    Intermediate VC to Table VC segue 是“Embed”

    我使用它而不是“导航控制器上的工具栏”解决方案,因为我正在将它添加到现有的具有大约 15 个屏幕的应用程序设计中,并且我不确定如何在不同的屏幕上配置不同的工具栏。

    【讨论】:

    • 我同意这不是很好,但它只是绕过了 IOS 的一个明显遗漏,即您不能在不先将该 UIVC 嵌入 UINavigationController 的情况下将 UIToolbar 添加到 UIViewController。在我的情况下,由于 UIC 的其他“设计特征”,我无法将我的 UIVC 嵌入到 UIC 中。
    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多