Add this switch method anywhere in your UIViewController. This hides from the first tap and is displayed again in the second tap.
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture { BOOL barsHidden = self.navigationController.navigationBar.hidden; [self.navigationController setNavigationBarHidden:!barsHidden animated:YES]; }
If there is no navigation controller, connect the navigation bar to IBOutlet and replace with
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture { BOOL barsHidden = self.navBar.hidden; self.navBar.hidden = !barsHidden; }
Then add the following in the method -(void)viewDidLoad {}
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleNavBar:)]; [self.view addGestureRecognizer:gesture]; [gesture release];
If the view you are going to click on is a UIWebViewController, you must add the protocol to the view controller and set it as a delegate gesture.delegate = self; and then add the following:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }
This is necessary because the UIWebViewController already implements its own gesture recognizers.
source share