Neelesh
Neelesh

Reputation: 3693

UIImageView tap to hide navigation bar

I have a view which shows an image from the web. The view only has an UIImageView. I want to know how to hide the navigationBar when the user taps and show it again when the user re-taps the view again. (Just like the native iPhone photo app)

I know i can use this

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

but i am not sure where to use this,where to put in this code.

Help would be appreciated

Upvotes: 1

Views: 1672

Answers (3)

Andrew Cook
Andrew Cook

Reputation: 116

If you can't figure out the other answers you can cheat a little bit. You can throw on a button set it to transparent and link a IBAction to it with the code:

UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake( x,y,0,0)];
imageButton.backgroundColor = [UIColor clearColor];

[imageButton addTarget:self action:@selector(navBarHide:) 
 forControlEvents:UIControlEventTouchUpInside];

-(IBAction)navBarHide {
if (!navBarHidden) {

[self.navigationController.navigationBar removeFromSuperView];

}
else {

[YourUIView addSubview: yourNavigationBar];

}
}

Upvotes: 0

Andrew
Andrew

Reputation: 7720

Initialize a new UITapGestureRecognizer:

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleNavigationBar:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.numberOfTouchesRequired = 1;
[self.imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];

You also must make sure the UIImageView has userInteractionEnabled set to YES because by default it is set to NO on UIImageView's.

self.imageView.userInteractionEnabled = YES;

Finally, write the method that is called when the gesture recognizer recognizes. This is the method selector that is passed in the action: argument in the gesture recognizer's initializer method:

- (void)toggleNavigationBar:(UITapGestureRecognizer *)tapGestureRecognizer
{
    [self.navigationController setNavigationBarHidden:![self.navigationController isNavigationBarHidden] animated:YES];
}

Upvotes: 3

Ivan
Ivan

Reputation: 340

Put a UITapGestureRecognizer on your UIImageView and in the delegate just call the method you mentioned. Something like this:

UITapGestureRecognizer* g = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
[img addGestureRecognizer:g];
[g release];

Then your delegate:

-(void) imageTapped:(UITapGestureRecognizer*)tg
{
    if(self.navigationController.toolbarHidden)
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    else
        [self.navigationController setNavigationBarHidden:NO animated:YES];
}

Upvotes: 2

Related Questions