views:

98

answers:

2

Hello to all,

I have an app with a navigation bar at the top. On one view that is a subclass of UITableView, I add a UIToolbar beneath the UITableView with the following code:

UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit]; // Set the toolbar to fit the width of the app
CGFloat toolbarHeight = [toolbar frame].size.height; // Calculate the height of the toolbar
CGRect rootViewBounds = self.parentViewController.view.bounds;
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
CGRect rectArea = CGRectMake(0, toolbarHeight, rootViewWidth, toolbarHeight);
[toolbar setFrame:rectArea];
[self.navigationController.view addSubview:toolbar];

The problem is that the toolbar is "on top" of the UITableView and is masking over the top of the contents of the first row in the UITableView. What I really want is for the table view to "start" beneath the UIToolbar.

How do I make this work appropriately?

Gracias, Jose

A: 

In my opinion, initializing a new toolbar is just the wrong way to go.

Just use this simple code since you already have an UINavigationController.

- (void)viewDidLoad {
    self.navigationController.toolbar.hidden = NO;
}

I'm sorry for the wrong code. Try this instead!

- (void)viewDidLoad {
    self.navigationController.toolbarHidden = NO;
}
nonamelive
Hmm. This didn't do anything in `viewDidLoad`, but if I attached this code to a button then it does work. Why would this be?
Jake Hansen
Hi, I have updated the code. Sorry for the wrong code. :)
nonamelive
A: 

I agree with nonamelive, but if there is a reason to need the logic as you described. You should make the view a plain view with both a UITableView subview and a toolbar subview and set their frames accordingly to position them as you want.

Another option if you wish for the toolbar to scroll along with the other table content is to make the table's headerView the toolbar.

NWCoder