Search code examples
objective-cuitableviewuinavigationcontrolleruitoolbar

Objective C: Using code to add a toolbar to a UITableView (within a Navigation Controller)


I managed to add a toolbar at the bottom of my UITableView using the code below:

toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
toolbar.frame = CGRectMake(0, 436, 320, 50);


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

[self.navigationController.view addSubview:toolbar];

However when I try to switch back to the first page of the navigation controller, the toolbar at the bottom of the page is still displayed. How can I ensure that the toolbar is only shown on the UITable View and not any other views in the navigation controller?

Thanks in advance.

Zhen


Solution

  • In your TableViewController implement:

    - (void)viewWillAppear:(BOOL)animated
    {
        self.navigationController.toolbar.hidden = NO;
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        self.navigationController.toolbar.hidden = YES;
    }