How do I add a UIToolBar to my UITableView view?

Discussion in 'iPhone/iPad Programming' started by jchildress, Sep 9, 2008.

  1. macrumors member

    Joined:
    Jul 9, 2008
    #1
    I've been trying to figure this out, but I'm running into some snags. I want to add a UIToolBar to the bottom of my UITableView. When I can get it to show up, the toolbar appears to be attached to the view and scrolls up and down with the table view. I want it to be anchored to the bottom of my view no matter how the TableView scrolls. I'm doing this like so, (please excuse my code writting as I'm at work and don't have xCode or my project available so my keywords are probably going to be wrong. I'm just trying to get the jist of my code across):

    Code:
    UITableViewController *myTableViewController = [[UITableViewController alloc] initWithTableStyle:UITableViewStyleGrouped];
    UIToolBar *myToolBar = [UIToolBar new];
    CGRect *toolBarFrame;
    toolBarFrame = CGMakeRect (0, 440, 320, 40);
    [toolBarFrame setFrame:toolBarFrame];
    [myTableViewController.view addSubView:toolBarFrame];
    Again, this code here is probably not 100% correct, but this is the sequence I used to add the Toolbar.

    Becuase this was not what I wanted, I tried creating a new XIB file that had a tableView in the view with a toolBar ontop at the bottom of the view. When I created my UITableViewController I initilized it with the xib, but when I try to load the view in code, it crashes and complains about not finding a tableview. So I went into my XIB and assigned the delegte's view to the tableview. That then worked to load the view, but the toolbar was missing. I assume becuase I was just loading the tableView view and not the contents of the entire view of the controller.

    What should be my sequence of events to add a toolbar that is anchored to the bottom of my tableView? I must be missing something here.

    Thanks,
    Jason
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Stop using UITableViewController. Use a standard UIViewController. Create a UIView. Add the UITableView to it sized to be the correct size (screen width, screen height-height of toolbar). Position it at top left. Add a UIToolbar to the UIView positioned at the bottom. Set the datasource and delegate of the UITableView correctly (probably as the UIViewController instance, i.e. self). Job done.
     
  3. thread starter macrumors member

    Joined:
    Jul 9, 2008
    #3
    I've spent some time looking into the dataSource method of filling in my UITableView with a UITableViewDataSource and for some reason I can't wrap my head around this concept to get it to work. I did some reading and looked at the example code Apple mentions in their online documentation and I can't seem to achive what they’re doing. I think, and I could be wrong, that Apple's examples are more complicated then what I need to use a dataSource for and maybe I shouldn't be using this.

    What I'm trying to achieve is a UIView that contains a UITableView and a UIToolBar where the UIToolBar is anchored to the bottom or top of the view and does not move when the UITableView is scrolled. It was mentioned that instead of using a UITableViewController, I should use the standard UIViewcontroller with a UITableView and UIToolBar in the view. This way the UIToolBar is independent of the UITableView's scrolling. After doing this, I can't get the UITableView to populate itself like it automatically does with a UITableViewController. Or I can't get it to populate when I supply it with the dataSource I created.

    Is there any other way to tell a table view to populate itself other then supplying a dataSource or using the initiation method "initwithStyle:"?

    I'm wondering if I can create a new UIViewController class that uses a <UITableViewDelegte>, add all of my UItableView functions to the *.m file and after creating\initializing the viewController I call [myViewController setStyle:UITableViewStyleGrouped];? The documentation for the the UITableView says that the "initWithStyle" command is the starting point for firing off all the other tableview commands that fills itself out. So I'm hoping that setStyle will do the same.

    If that doesn't work. can anybody recommend some example code that is a good and very simple example of creating and using a UITableViewDataSource delegate? I looked into the Apple example code "the Elements" and "UI Controls" and didn't understand it to well. Maybe there was something in those examples that I over looked.

    Thanks,
    Jason
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    If you post you code then perhaps we can find out what you are missing. At the moment it's just a guessing game...
     
  5. thread starter macrumors member

    Joined:
    Jul 9, 2008
    #5
    I'll post it tonight when I get home from work.
     
  6. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    I've attached a project that shows a tableview and toolbar in the same view. The layout is done in IB. I don't use a UITableViewController but I'm pretty sure there are some ways to do that also.
     

    Attached Files:

  7. macrumors newbie

    Joined:
    Mar 8, 2010
    #7
    Try this...it worked for me

    Code:
    (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];
    
    }
     

Share This Page