Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar in Navigation Controller

I am creating a toolbar in a Navigation Controller using the following code:

 [self.navigationController setToolbarHidden:NO];

 //Create a button
     NSArray *toolbarItems = [NSArray arrayWithObjects:
                              [[UIBarButtonItem alloc] initWithTitle:@"Help" style:UIBarButtonItemStyleBordered target:self action:@selector(helpButton:)]
                              ,nil];

The only problem is that the toolbar is visible whenever there is a navigation controller(multiple other views). Is there a way to only restrict the toolbar to a single view?

Thanks

like image 571
Ahan Malhotra Avatar asked Feb 23 '23 06:02

Ahan Malhotra


1 Answers

To quote the UINavigationController Class Reference:

The navigation toolbar is hidden by default but you can show it for your navigation interface by calling the setToolbarHidden:animated: method of your navigation controller object. If not all of your view controllers support toolbar items, your delegate object can call this method to toggle the visibility of the toolbar during subsequent push and pop operations.

So, set a delegate for your navigation controller. In your delegate's navigationController:willShowViewController:animated:, do something like this:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    BOOL shouldShowToolbar = (viewController == theViewControllerThatNeedsAToolbar);
    [navigationController setToolbarHidden:shouldShowToolbar animated:animated];
}
like image 199
rob mayoff Avatar answered Mar 05 '23 07:03

rob mayoff