Night Hunter
Night Hunter

Reputation: 123

How can i hide/show my navigation bar (Not navigation controller) when tapped on view?

I have one navigation bar in my scroll view.(Use StoryBoard)
I want to hide my navigation bar when user tap on view.
When user tap again, the navigation bar will show.
How can i make it?

Upvotes: 2

Views: 1165

Answers (1)

Rob
Rob

Reputation: 437392

If you're using a navigation bar (without a controller), you have to animate the changing of the frame of the navigation bar, as well as that of the scroll view. In the example below, I'm just shifting the navigation bar off the top of the screen and adjusting the size of the scroll view accordingly. You obviously need IBOutlet references for both the navigation bar as well as the scroll view:

@interface ViewController ()

@property (nonatomic) BOOL navigationBarHidden;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationBarHidden = NO;

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self.scrollView addGestureRecognizer:gesture];
}

- (void)handleTap:(id)sender
{
    [UIView animateWithDuration:0.5
                     animations:^{
                         CGRect navBarFrame = self.navBar.frame;
                         CGRect scrollViewFrame = self.scrollView.frame;

                         if (self.navigationBarHidden)
                         {
                             navBarFrame.origin.y += navBarFrame.size.height;
                             scrollViewFrame.size.height -= navBarFrame.size.height;
                         }
                         else
                         {
                             navBarFrame.origin.y -= navBarFrame.size.height;
                             scrollViewFrame.size.height += navBarFrame.size.height;
                         }

                         self.scrollView.frame = scrollViewFrame;
                         self.navBar.frame = navBarFrame;

                         self.navigationBarHidden = !self.navigationBarHidden;
                     }];
}
@end

If you're using autolayout, it's slightly different (you have to animate the changing of the constraints), but the basic idea would be the same. Let me know if you're targeting iOS 6 and higher only and are using autolayout.


If you're using a navigation controller, it's a little easier, as you can hide with setNavigationBarHidden:

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

You can show with:

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

If you want to do this on a tap, you can do something like (you'll need an IBOutlet to your scroll view for this):

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self.scrollView addGestureRecognizer:gesture];
}

- (void)handleTap:(id)sender
{
    BOOL hidden = self.navigationController.navigationBarHidden;

    [self.navigationController setNavigationBarHidden:!hidden animated:YES];
}

Upvotes: 3

Related Questions