Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting tap to Show/hide UINavigationBar

I am kinda new to iPhone development and haven't done anything yet envolving touches. My view hierarchy like this:

UIView - UIImageView - UIScrollView - CustomView

How do I detect if the user has tapped anywhere on the screen so I can show/hide the navigation bar accordingly? I don't need user interaction on my CustomView but I'd like to ignore touches on the UIScrollView when the user just wants to drag it.

I can already show/hide the navigation bar from my view controller programatically using:

[self.navigationController setNavigationBarHidden:YES animated:YES];

Thanks in advance!

like image 974
leolobato Avatar asked Aug 14 '09 15:08

leolobato


2 Answers

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHideNavbar:)];
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];

-(void) showHideNavbar:(id) sender { // write code to show/hide nav bar here }

This is the way to do it using UIGestureRecognizers available on iOS4

like image 50
Mugunth Avatar answered Nov 08 '22 16:11

Mugunth


You can use the method touchesBegan in UIView to detect the tap, so you will have to have a custom subclass of UIView for the viewcontroller's view that you would like to detect taps on. Then you'll have to use the delegate to message your view's controller so it can hide the navigationBar.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSUInteger numTaps = [[touches anyObject] tapCount];
    if (numTaps == 1)
    {
        [delegateController tapDidOccur];  
    }
}
like image 33
Daniel Avatar answered Nov 08 '22 17:11

Daniel