How to add a UIToolbar to a UITableViewController programmatically?
Asked Answered
L

3

32

I have opted to use a UITableViewController without a nib. I need a UIToolbar at the bottom with two buttons. What is the simplest way of doing that?

P.S. I know that I can easily use a UIViewController and add a UITableView however I want things to look consistent across the app.

Can someone help?

I saw the following example and I am not sure on its validity:

(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    //Initialize the toolbar 
    toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = UIBarStyleDefault;

    //Set the toolbar to fit the width of the app. 
    [toolbar sizeToFit];

    //Caclulate the height of the toolbar 
    CGFloat toolbarHeight = [toolbar frame].size.height;

    //Get the bounds of the parent view 
    CGRect rootViewBounds = self.parentViewController.view.bounds;

    //Get the height of the parent view. 
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

    //Get the width of the parent view, 
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

    //Create a rectangle for the toolbar 
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

    //Reposition and resize the receiver 
    [toolbar setFrame:rectArea];

    //Create a button 
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"back"
                                                                   style:UIBarButtonItemStyleBordered 
                                                                  target:self 
                                                                  action:@selector(info_clicked:)];

    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];

    //Add the toolbar as a subview to the navigation controller.
    [self.navigationController.view addSubview:toolbar];

    [[self tableView] reloadData];
}

(void) info_clicked:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];
    [toolbar removeFromSuperview];

}
Lawannalawbreaker answered 11/5, 2011 at 3:38 Comment(1)
Need to point that most important line here is [toolbar sizeToFit]; without it - toolbar is displayed, but not accepting any user interactionsArmourer
C
78

The simpler thing to do is to build your project on top of a UINavigationController. It already has a toolbar, it's just hidden by default. You can reveal it by toggling the toolbarHidden property, and your table view controller will be able to use it as long as it's in the navigation controller hierarchy.

In your app delegate, or in the object your app delegate passes control to, create the navigation controller with your UITableViewController as the root view controller:

- ( void )application: (UIApplication *)application
          didFinishLaunchingWithOptions: (NSDictionary *)options
{
    MyTableViewController         *tableViewController;
    UINavigationController        *navController;

    tableViewController = [[ MyTableViewController alloc ]
                                 initWithStyle: UITableViewStylePlain ];
    navController = [[ UINavigationController alloc ]
                           initWithRootViewController: tableViewController ];
    [ tableViewController release ];

    /* ensure that the toolbar is visible */
    navController.toolbarHidden = NO;
    self.navigationController = navController;
    [ navController release ];

    [ self.window addSubview: self.navigationController.view ];
    [ self.window makeKeyAndVisible ];
}

Then set the toolbar items in your MyTableViewController object:

- ( void )viewDidLoad
{
    UIBarButtonItem            *buttonItem;

    buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Back"
                                            style: UIBarButtonItemStyleBordered
                                            target: self
                                            action: @selector( goBack: ) ];
    self.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
    [ buttonItem release ];

    /* ... additional setup ... */
}
Cystocele answered 11/5, 2011 at 4:5 Comment(1)
This is an amazing piece of advice. I never knew that UINavigationController has a toolbar that is hidden by default. I was already using it so this was a real bonus. Also thanks for taking the time to really explain everything.Lawannalawbreaker
D
6

You also can just check "shows toolbar" option in NavigationController attributes inspector.

Dormant answered 24/3, 2013 at 10:30 Comment(1)
But then it is turned on on all views which ends up in using code anyway on those views where toolbar should not be used.Capacitance
S
1

Here is a simple example, which may help

UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteMessages)];
    UIBarButtonItem *composeItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(composeMail)];
    NSArray *toolbarItems = [NSMutableArray arrayWithObjects:spaceItem, trashItem,spaceItem,composeItem,nil];
    self.navigationController.toolbarHidden = NO;
    [self setToolbarItems:toolbarItems];

Thanks, prodeveloper

Siloum answered 29/5, 2014 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.