How to hide navigation bar in iPhone? - iphone

Currently i am working in iPhone app, I have two screen like A and B, A has no navigation bar, but B has navigation bar. so i set like this.
Class A:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"A";
[self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES];
}
Class B:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"B";
[self.navigationController setNavigationBarHidden:NO];
}
-(void)Previousscreen
{
[self.navigationController popViewControllerAnimated:YES];
}
then i run the application, When i go to previous class like B to A at the time blue color show in B class below attached screen shot for your reference. How to fix this issue? please help me
Thanks in Advance

Set it in class B
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO];
}

You'll need to use this code:
[navigationController setNavigationBarHidden: YES animated:YES]
in - (void)viewWillAppear:(BOOL)animated or later in the view lifecycle in both classes. [Avoid doing this in - (void)viewDidLoad.]
The trick here is in using the setNavigationBarHidden:animated: method (in place of the simpler setNavigationBarHidden: method). This will ensure your UI issue goes away and also any positional issues due to it.
P.S. Check the value of self.navigationController.navigationBarHidden (instead of self.navigationController.navigationBar.hidden) if you need to check if your navigation bar is hidden, at some point, in your code.

I don't think a behavior when you are hiding and showing the navigation bar dynamically as you are pushing controllers is supported.
Simple solution - hide the animation bar of the UINavigationController and if you want to show it on some controller, just add a UINavigationBar to it.

Use below line to hide navigationBarin viewWillAppear: method -
-(void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden=YES;
}

Try setting the navigationBarHidden: in viewWillDisAppear of class B
in class B
-(void)viewWillDisAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES];
}

Your code is okay for Hide and Unhide the navigationBar. The problem is that you're hiding Class A's navigationBar in viewWillAppear: which is called just before appearing the view so before loading the Class A view navigationBar is being hidden each time.
And if we talk about your blue color i think it is your window color. Because after hidden the navigationBar there will be a space above your self.view which height is 44.0. So there are three options to fixed it.
Hide Class A's navigationBar in Class A's viewDidAppear: method.
Set your window color what you want to show.
You can add an image to window background in which at top of image make a navigationBar same as Class B's navigationBar so when the original navigationBar will be hide it will see.

I've had to solve this recently and I found that it was necessary to call setNavigationBarHidden:NO immediately after pushViewController: and setNavigationBarHidden:YES immediately after popViewController:, with animated YES in each call.
So, when pushing:
[nc pushViewController:classBView animated:YES]
[nc setNavigationBarHidden:NO animated:YES]
and when popping:
[nc popViewControllerAnimated:YES]
[nc setNavigationBarHidden:YES animated:YES]
But in my case, while I could do pushing as above, I didn't want to alter my class B and instead wanted it to not know of care that the navigation bar wasn't previously hidden (since its not my code). Also, that view gets popped using the normal Back button, there was no explicit call to popViewControllerAnimated:. What was going to work best in my code was to make my class A be the UINavigationController delegate and hide the toolbar on a delegate method call when the pop occurs.
Unfortunately I found that the UINavigationControllerDelegate methods weren't too helpful, willShowViewController & didShowViewController are called indistinguishably when pushing my class B view or when popping back to it from another one that it has pushed.
I followed a suggestion in https://stackoverflow.com/questions/642312/ about overriding UINavigationController and I made some custom delegate methods, one is called right after [super popViewControllerAnimated:]. My subclass is available at https://gist.github.com/jpmhouston/6118713 and delegate method is:
- (void)navigationController:(UINavigationController *)navigationController isPoppingViewController:(UIViewController *)poppedViewController backTo:(UIViewController *)revealedViewController {
if (revealedViewController == self && [poppedViewController isKindOfClass:[MyClassB class]]) {
[navigationController setNavigationBarHidden:YES animated:YES];
// ...and more code to run only when going from class B back to class A
}
}
I'm sure there are simpler ways to have setNavigationBarHidden: called following the Back button being pressed, but this worked for me.

Related

What method will be called when Navigation back button is clicked -IOS?

This questions look repeated, but I din't got proper solution for my problem. In my case, Once I clicked back button I want to hide the navigation bar. For e.g. View1 pushed view2, view2 will have navigation bar,once I clicked back it brings me back to view 1 it should not contain navigation bar I need to hide it. I tried with viewWillDisappear method in view 2 to hide, it worked but if I have more view and I'm pushing each view from view1 if I click back button,navigation bar should hide in view1. So is anyway is there to know in view 1 itself that other view is popped. I tried viewwillappear method in view1 its not called. What can I do here.?
This method got called once I initially load view1, not after popping view2..
- (void) viewWillAppear : (BOOL)animated
{
[root_obj.navigationController setNavigationBarHidden:YES animated:YES];
}
This is how I am pushing view 2..
[root_obj.navigationController pushViewController:view2 animated:NO];
thanks in Advance
i think, u have forgot to call viewWillAppear: method of super class.
-(void) viewWillAppear : (BOOL)animated
{
[super viewWillAppear:animated];
[root_obj.navigationController setNavigationBarHidden:YES animated:YES];
}
Why don't you hide the NavigationBar in View1's ViewDidAppear method
Edit
I am not sure what the problem with your code but you can do these kind of things in ViewWillAppear or ViewDidAppear methods for more information UIViewController Class Reference and check ViewWillAppear or ViewDidAppear

setNavigationBarHidden:YES doesn't work with the searchDisplayController

I'm using the following code to hide my navigationBar in the detailViewController(my second view),
and it works perfectly fine when I tap any of my object from the MasterViewController(my first view).
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
However, when I filter the table list in the masterViewController using searchDisplayController
and tap any object from the result, the navigationBar in the detailView doesn't get hidden...
Do I have to do any extra work to hide the navigationBar if I use the searchDisplayController?
for Debug, I set the break point on the line of setNavigationBarHidden:YES, and even if
I go to the detailViewController via search result, the application hits the line..
you shuold put [self.navigationController setNavigationBarHidden:YES]; in viewWillLayoutSubviews function.like this:
- (void) viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self.navigationController setNavigationBarHidden:YES];
}
it works.
You should try this method:
In that controller, where you declared UISearchController *searchController, you should implement two methods (only for example):
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// if you want to hide Navigation Bar when searchController will become active
_searchController.hidesNavigationBarDuringPresentation = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
_searchController.hidesNavigationBarDuringPresentation = NO;
}
The code above may have differences. Main point in hidesNavigationBarDuringPresentation property (iOS 8.0 and later). Try to play with it, and turn to hidesNavigationBarDuringPresentation = NO before pushing a new controller. After this manipulations I took profit: when pushed UIViewController, setter setNavigationBarHidden:YES become working
if you want to hide Navigation bar then, In your MainWindow xib uncheck "Shows Navigation Bar" attributes of Navigation Controller.
This Will hide the Navigation Bar in your Whole Project. If you want to Show Navigation Bar in any Controller set NavigationBar Hidden = NO in ViewDidLoad Method of that Controller.
you should hack search display controller in some way to hide its built in navigationBar.
here is the answer:
https://stackoverflow.com/a/6337037/1348121
This
- (void) viewWillLayoutSubviews
causes layout problems, so i used code below. Works fine for me.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO];
}

How to hide/disable only the first uinavigationbar?

I've been wandering how to hide / remove / disable only the main or first navigation bar in the navigation controller so that I could put an image as a whole background screen but I couldn't find any solution.
Did try hide the titleview in viewdidLoad of the main navigation controller but didn't work. Tried using navigationBarHidden but it hides the whole navigation bar for the next stack of controller.
So, I'm not sure how to do this. To give you an example, I would like to have something like this app - The Masters Golf Tournament - http://appshopper.com/sports/the-masters-golf-tournament.
If you look at Screen 1, it doesn't have any nav bar at the top but when you touch any options it will push to a new view controller and have the nav bar appear as in Screen 3,4 and 5.
Hope anyone could help me with this.Thanks a lot!
In most of my applications I have a custom UIViewController class that I derive all other custom controllers from. In some of these, I added a method like navigationBarInitiallyHidden to the base class that other classes can override. The default result depends on the nature of the application.
In the delegate of the navigation controller, when a controller is being shown that implements that method, the delegate hides or shows the navigation controller accordingly. Since I animate the hide or show, I check the current state and do nothing if no change is needed.
You could do something simpler in your delegate method. If the controller being shown is the root controller, hide the navigation bar, otherwise show it if it is hidden.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ( viewController == rootController ) {
[navigationController setNavigationBarHidden:YES animated:animated];
} else if ( [navigationController isNavigationBarHidden] ) {
[navigationController setNavigationBarHidden:NO animated:animated];
}
}
You can hide the navigation bar:
[self.navigationController setNavigationBarHidden:YES];
and where you want to show the navigation bar again
[self.navigationController setNavigationBarHidden:NO];
hide/disable
self.navigationController.navigationBarHidden = YES;
show/Enable
self.navigationController.navigationBarHidden = NO;
You can hide navigation bar by using bellow code. Below code will hide navigationbar at the time of viewWillAppear.
Objective C
-(void)viewWillAppear:(BOOL)animated
{
[[self navigationController] setNavigationBarHidden:YES animated:NO];
}
Swift
self.navigationController?.setNavigationBarHidden(true, animated: animated)

Keeping UINavigationController's navigationBar hidden after UISearchDisplayController selection

I have a UISearchDisplayController setup with a UITableViewController which is nested inside a UINavigationController. When a selection of a cell is made, UITableView's didSelectRowAtIndexPath method is triggered, which pushes a new view to the parent navigation controller. This new view should have the navigation bar hidden on entry.
[[self navigationController] setNavigationBarHidden:YES animated:NO];
I use this line in the didSelectRowAtIndexPath method to hide the navigation bar. This works fine when a row is selected not using the search controller, but is overridden when selecting a search result. It seems the UISearchDisplayController takes it in its right to un-hide the navigationBar sometime after the row is selected.
If I move the setNavigationBarHidden call into the target view's viewWillAppear method, results are similar. I can make it work by placing the hide call in viewDidAppear, but this makes for a very awkward transition effect which feels jumpy and out of place. I would like to make the navigationBar already hidden before the new view slides on to the screen.
Does anyone know where the unhiding of the navigationBar is occurring, and/or any way I can override this behaviour?
This may not be the most elegant solution, but I believe it does exactly what you'd want it to. I came across a similar problem, and my solution was to have a method which hides the navigation bar, which is called after a delay of 0 seconds as follows.
The method that is called is:
-(void) hideNavBar {
if (self.navigationController.navigationBar.hidden == NO)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
}
Then in the viewDidLoad method, I have the following:
[self performSelector:#selector(hideNavBar) withObject:nil afterDelay:0.0];
This works and removes the navigation bar in one instantaneous swoop. You can amend the delay time if you want the animation or for it to be removed after a delay. I tried [self hideNavBar] but that simply did not work, so sticking to what I have above.
Hope this helps, and if someone has a more elegant solution, I'm interested!
Ok, this bugged me for the a couple of hours, but I finally got it to work! The problem seems to be that the UISearchDisplayController keeps track of whether or not it has hid the navigation bar, and if it has, it restore it, after the view has been dismissed. That is why with many of the answers above you see the tail end of the animation of the bar hiding itself when the new view is pushed. However, by tricking the search display controller we can change this behavior.
First: Subclass The UISearchDisplayController
Following the answer on how to keep a navigation controller from hiding, found here, I altered the code, to keep the navigation bar hidden:
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if(self.active == visible)
return;
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:YES];
[super setActive:visible animated:animated];
if (visible)
[self.searchBar becomeFirstResponder];
else{
[self.searchBar resignFirstResponder];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:YES];
}
}
Note we hide the navbar before we call the super setActive function. This seems to keep the super class from trying to hide the nav bar and consequently, from trying to restore it ater item selection. Now when the controller becomes active, the bar will be hidden like normal. Also note that we restore the navigation bar when the searchBar resigns first responder. This will restore the bar if we cancel out of the controller.
Second: Hide Navigation Bar When Exiting
If we hide the navigation bar in the view will disappear, it will be hidden:
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
Third: Hide Navigation Bar When Returning
The only problem now is that if we select a row from the filtered tableview, when we return, the navigation bar will be visible. To fix this we need to put a check in view will Appear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.isFiltered){
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
}
While this feels like a huge hack, it does the trick and I could see no better way of doing it.
Bumped into the same problem, managed to get it working smoothly with this ugly hack:
- (void) viewWillDisappear: (BOOL) animated
{
if (searchController_.active)
{
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
self.navigationController.navigationBar.tintColor = nil;
}
[super viewWillDisappear: animated];
}
- (void) viewWillAppear: (BOOL) animated
{
if (searchController_.active)
{
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}
[super viewWillAppear: animated];
}
I had the same problem: my view has the navigation bar hidden by default and here's the way to keep it hidden:
-(void) viewWillLayoutSubviews{
if (self.navigationController.navigationBar.hidden == NO)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
}
This way the navigation bar doesn't appear even after the search bar has been used.
- (void) viewWillDisappear: (BOOL) animated
{
// self.searchOn property tell if full screen search is enabled
//if (self.searchOn)
//{
[self.navigationController setNavigationBarHidden:NO animated:NO];
//}
[super viewWillDisappear: animated];
}
- (void) viewWillAppear: (BOOL) animated
{
//if (self.searchOn)
//{
[self.navigationController setNavigationBarHidden:YES animated:YES];
//}
[super viewWillAppear: animated];
}

How does presentModalViewController interact with nested UITabBarController and UINavigationController

I have a view that I want to take up the full screen, so I override the init method, and some of the view methods:
- (id) init {
if (self = [super init]) {
self.wantsFullScreenLayout = YES;
}
return self;
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
}
Now, from another screen, I want to display it as a modal view:
UIViewController *screen = [[MyScreen alloc] init];
[self presentModalViewController:screen];
[screen release];
All pretty standard stuff. When I want the full-screen view to go away, however, the previous view is shifted or stretched up by about 40 pixels.
Specificially, I have a UITabBarController with a UINavigationController inside, displaying a UITableViewController, which is the view that displays the subview, and also the view that gets shifted up. If the table is not in a navigation controller, everything works just fine, nothing gets shifted up at all. If I experiment with commenting out the wantsFullScreenLayout and setStatusBarHidden lines with no navigation bar, it sometimes shifts up just 20 pixels, or doesn't actually display on the full screen (but later it does without changing any code), or sometimes doesn't break at all (but I am not getting the full full screen with any of these)
What am I doing wrong?
Through some combination of Sean's suggestion and jumping up the responder chain, I've found a solution that works is what seems like all circumstances (so far).
First issue:
The Table View by itself does not display in a navigation controller, but may show up in one if being selected from the more view in the tab bar, and that's the case where displaying the modal view in full screen causes the table to underlap the navigation bar upon return.
Second issue:
When not displayed in a navigation controller, presenting the modal view does not take up the full screen (even though wantsFullScreenLayout is set to YES). When returning from this view, the view is shifted up by 20 pixels and you can see a gap between the bottom of the table and the top of the tab bar.
Solution:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void) presentModalViewController:(UIViewController *)screen animated:(BOOL)animated {
UIResponder *responder = self;
while (responder && ![responder isKindOfClass:[UITabBarController class]]) {
responder = [responder nextResponder];
}
[(UIViewController *)responder presentModalViewController:screen animated:YES];
}
The toggling of the navigation bar's visibility forces the relayout. Overriding presentModalViewController actually calls presentModalViewController on the tab bar controller instead, which then causes it to show in the full screen. For some reason, self.tabBarController is nil when not in the more view controller, so I had to jump up the responder chain to find it.
Your UINavigationController will get called with the viewWillAppear before the modal view is dismissed. Have you tried calling [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO]; inside the controllers that can be visible post modal dismissal. I have run into tons of problems displaying modal views on top of UINavigationControllers when bounds change. It fights any layout changes and requires lots of resetting to previous states to get it behaving nicely. It might also not hurt to call [self.navigationController setNavigationBarHidden:NO animated:NO] as well to force layout.
If this works well it might serve you to create a simple baseclass that sets these in it's viewWillAppear and then just subclass it for all non modal view controllers.
If this doesn't work you might try placing a swap view at the top level that contains the tab bar controller and then you could remove the tab bar controller with a transition when you present your modal view. Yes this isn't technically modal but would still look nice and offer the same effect. At that time since the view controller is out of the view hierarchy it shouldn't get it's layout all munged.
I think this has to do with the timing of the presentModalViewController: call. As a test you could try adding sleep(3) before you call that method. If that fixes anything, or even if it doesn't i guess I would try moving the order of things around. maybe viewDidDisappear and viewDidAppear instead of 'Will'