Hide / show UINavigationbar on screen tap

I am very new to iOS Development, and I just created one of my first applications in my .xib file. I have a UINavigationBar that I want to hide / show when a part of the screen will be listened to by the user (for example, in the Photos application). I found several snippets on the Internet, but I don’t know where and how to use them.

I would really appreciate it if someone could give me detailed information on how to do this.

+4
source share
2 answers

Add this switch method anywhere in your UIViewController. This hides from the first tap and is displayed again in the second tap.

 - (void)toggleNavBar:(UITapGestureRecognizer *)gesture { BOOL barsHidden = self.navigationController.navigationBar.hidden; [self.navigationController setNavigationBarHidden:!barsHidden animated:YES]; } 

If there is no navigation controller, connect the navigation bar to IBOutlet and replace with

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

Then add the following in the method -(void)viewDidLoad {}

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

If the view you are going to click on is a UIWebViewController, you must add the protocol to the view controller and set it as a delegate gesture.delegate = self; and then add the following:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } 

This is necessary because the UIWebViewController already implements its own gesture recognizers.

+21
source

Ultimately, you want to send the -setHidden: message to the navigation bar. The easiest way to do this is to do Outlet and Action in your view controller. Then, in the .xib file, connect the navigation bar to the outlet and some button (even a large, full-screen) for the action.

Sockets and actions are the main methods used many times in iOS (and Mac), so if you do not understand them, it is best to read them now. Each initial iOS / Mac programming book covers this, like Apple 's Getting Started Guide (pay particular attention to the "View Setup" section).

Inside your action, send a message to the outlet as follows:

 -(void)myButtonAction:(id)sender{ [[self myNavigationBarOutlet] setHidden:YES]; } 

This will hide the navigation bar whenever your button is pressed.

(It is assumed that you have a UINavigationBar in your .xib, as you say. These directions will be different if you work with the UINavigationController , which manages its own UINavigationBar )

0
source

Source: https://habr.com/ru/post/1382154/


All Articles