如何在UITableView object -c/ios/xcode的底部添加工具栏

时间:2022-09-06 20:30:02

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的工具栏?比如聊天或短信应用。提前谢谢你. .

4 个解决方案

#1


2  

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

#2


26  

I just found a better trick!

我发现了一个更好的方法!

  1. Make sure there is NO Navigation Bar (from your failed attempts)
  2. 确保没有导航栏(来自失败的尝试)
  3. Drag and drop a "Bar Button Item" (Xcode will magically place it for you at the bottom)
  4. 拖放一个“Bar按钮项”(Xcode会神奇地将它放在底部)
  5. NOTE: If you Run the App now you won't see anything! (so keep reading)
  6. 注意:如果你现在运行这个应用,你什么都看不到!(继续阅读)
  7. Add the following line of code under viewDidLoad:
  8. 在viewDidLoad下添加以下代码行:

self.navigationController.toolbarHidden = NO;

self.navigationController。toolbarHidden =没有;

Done!

完成了!

#3


5  

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.

这将处理布局视图和动画键盘外观。您将需要为表视图和文本字段处理委托和数据源方法。

#4


1  

It's not great in some ways, but I solved this by using a Container View on an additional View Controller.

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

如何在UITableView object -c/ios/xcode的底部添加工具栏

The Intermediate VC to Table VC segue is "Embed"

中间VC到表VC的segue是Embed

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.

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

#1


2  

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

#2


26  

I just found a better trick!

我发现了一个更好的方法!

  1. Make sure there is NO Navigation Bar (from your failed attempts)
  2. 确保没有导航栏(来自失败的尝试)
  3. Drag and drop a "Bar Button Item" (Xcode will magically place it for you at the bottom)
  4. 拖放一个“Bar按钮项”(Xcode会神奇地将它放在底部)
  5. NOTE: If you Run the App now you won't see anything! (so keep reading)
  6. 注意:如果你现在运行这个应用,你什么都看不到!(继续阅读)
  7. Add the following line of code under viewDidLoad:
  8. 在viewDidLoad下添加以下代码行:

self.navigationController.toolbarHidden = NO;

self.navigationController。toolbarHidden =没有;

Done!

完成了!

#3


5  

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.

这将处理布局视图和动画键盘外观。您将需要为表视图和文本字段处理委托和数据源方法。

#4


1  

It's not great in some ways, but I solved this by using a Container View on an additional View Controller.

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

如何在UITableView object -c/ios/xcode的底部添加工具栏

The Intermediate VC to Table VC segue is "Embed"

中间VC到表VC的segue是Embed

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.

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