简体   繁体   中英

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!

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

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];  
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM