Search code examples
storyboarduitoolbarviewcontrollerviewdidloaduitapgesturerecognizer

show/hide UIToolbar with tap


I´m using a storyboard with different views for a book-like app. In every view I want a toolbar with button that appears on tap, that gives the user the alternative to return to main view. I have achieved that with a UITapGestureRecognizer. However with the current code my Toolbar is not hidden when i go to the view. When I tap it disappears like I want. How can I switch so that the toolbar is hidden by default, and on tap it appears?

Here is my code:

@implementation secondViewController 

- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
BOOL barsHidden = self.topBar.hidden;
self.topBar.hidden = !barsHidden;
}

- (void)viewDidLoad
{
[super viewDidLoad];
 UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self    
action:@selector(toggleNavBar:)];
[self.view addGestureRecognizer:gesture];


}

Solution

  • Hide the UIToolBar when your view loads like this

    - (void)viewDidLoad  
    {  
     [super viewDidLoad];  
     UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleNavBar:)];   
     [self.view addGestureRecognizer:gesture];
     self.topBar.hidden = YES;
    }