hidesBottomBarWhenPushed but when popped - iphone

I've a problem with something that seems to be very simple.
My app has a view hierarchy consisting in a UITabBarController containing UINavigationControllers. When I navigate from the root to the second level
I set the hidesBottomBarWhenPushed on true so that the tab bar is hidden
On my firstLevelController:
[secondLevelController setHidesBottomBarWhenPushed:YES];
[self.navigationController pushViewController:secondLevelController animated:YES];
After that when I push to the third level, I bring the tab bar again by doing in the secondLevelController:
[self setHidesBottomBarWhenPushed:NO];
[thirdLevelController setHidesBottomBarWhenPushed:NO];
[self.navigationController pushViewController:thirdLevelController animated:YES];
(I know, I didn't like the [self setHidesBottomBarWhenPushed:NO] either, but it didn´t work otherwise...)
So, here is the problem: when I push the back button in the third level and the second view appears, I need to hide the tabbar again but I couldn´t find the way of doing this.
Any help is appreciated

This is what works for me.
[self setHidesBottomBarWhenPushed:NO];
[thirdLevelController setHidesBottomBarWhenPushed:NO];
[self.navigationController pushViewController:thirdLevelController animated:YES];
[self setHidesBottomBarWhenPushed:YES];
The thirdlevelController shows the tabbar and secondLevelController does not show the tabbar when you pop the thirdLevelController.

On your secondViewController, do :
- (BOOL) hidesBottomBarWhenPushed {
return ([self.navigationController.viewControllers lastObject] == self);
}
This way, the tabbar will always be hidden when you are on the secondViewController, and it will appear on the other view controllers

You can hold a bool value to understand if you are coming from a popViewController
and in viewDidAppear you can detect it an hide your tab bar again.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if(backFromThirdView)
[self setHidesBottomBarWhenPushed:YES];
else
[self setHidesBottomBarWhenPushed:YES];
}

I was actually on the same problem. I always tried to hide the tabbar when selecting a row and to disable hiding after returning to the list (a tableview inside a navigationcontroller) so that the user can select the menu again. I set the tabbarcontroller hidden inside the method
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
but when I hided it inside this method, the Tabbar was still hided when returning to my list again. Now I hide the Tabbarcontroller inside the init method of a specific viewcontroller, maybe this works for somebody else too:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
[self setHidesBottomBarWhenPushed:YES];
return self;
}
now when i select a list item and this viewcontroller will be presented the tabbar is hided, after returning to the list it appears again.

You can try this
You declare in the secondLevelController
static BOOL bottomBarShouldHide = YES;
In the viewDidLoad,
if (bottomBarShouldHide) {
[secondLevelController setHidesBottomBarWhenPushed:YES];
bottomBarShouldHide = NO;
}
else {
[secondLevelController setHidesBottomBarWhenPushed:NO];
bottomBarShouldHide = YES;
}
I hope it could help you.

Related

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];
}

UINavigationController: Hiding Back Button on One View Hides it For All Views

I have a UINavigationController that contains 3 UIViewControllers on the stack.
View A - is the root
View B - is pushed by View A and has `self.navigationItem.hidesBackButton = YES;`
View C - is pushed by View B and has `self.navigationItem.hidesBackButton = NO;`
View C does not show the back button, even though I have hidesBackButton set to NO. How can I resolve this?
Update
A possible bug in 4.2 as it works till 4.1 sdks
I have tried this and mine is working perfectly. I am just posting the implementation of B view controller (BVC) and C view controller (CVC). My initial guess is that you are not setting the title of BVC in viewDidLoad.
#implementation BVC
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"I am B";
}
- (void) viewWillAppear:(BOOL)animated{
self.navigationItem.hidesBackButton = YES;
}
- (IBAction)pushB:(UIButton *)sender{
CVC *cvc = [[CVC alloc] initWithNibName:#"CVC" bundle:nil];
[self.navigationController pushViewController:cvc animated:YES];
[cvc release];
}
#end
#implementation CVC
- (void) viewWillAppear:(BOOL)animated{
self.navigationItem.hidesBackButton = NO;
}
#end
I think you have to set that property before you push or pop a view controller to affect the next view controller, setting it for the current viewcontroller in viewWillAppear is too late.
Edit: this looks like a bug in 4.2! The back button remains hidden both in the 4.2 simulator and on the device with 4.2, but it works in the 3.2, 4.1, and 4.0 simulators!
Here's the code where when pushing a VC with a hidden back button:
- (IBAction) goto2nd
{
SecondVC *vc = [[[SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil] autorelease];
vc.navigationItem.hidesBackButton = YES;
[self.navigationController pushViewController:vc animated:YES];
}
That is all that should be needed, each VC has its own navigationItem, it's not a global setting, so you don't need to bother undoing it to restore the back button (at least when popping back to a VC where it is set to "NO").
Here's a workaround that I'm using successfully on 4.3.
Instead of hiding the back button, set the left bar button view to an empty view:
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectZero];
UIBarButtonItem *tmpButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tmpView];
[tmpView release];
self.navigationItem.leftBarButtonItem = tmpButtonItem;
[tmpButtonItem release];
To restore the back button, just set the left bar button item to nil:
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
Update: It appears as if the bug is gone in 4.3.
Note: Even though the bug seems to be fixed, I prefer the "empty view" technique because it allows the disappearance and reappearance of the back button to be animated.
The solution for this problem is somewhat tricky..just try it it will surely work since even I faced the same problem.
First set Navigation title in viewWillAppear.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationItem.title = #"SET YOUR TITLE";
}
When you are navigating to other page just set your navigation title to null.This will not show you any button on top.Since you can get rid of writing
self.navigationItem.hidesBackButton = YES; everytime.
- (IBAction)pushB:(UIButton *)sender
{
SecondVC *vc = [[[SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil] autorelease];
self.navigationItem.title = #"";
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
I'm running the same issue and it's only happening on the iOS 4.2 simulator, so probably it's a bug on that version.
Reedit:
Try with this, it worked for me:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.navigationItem.hidesBackButton = NO;
}
Use the UINavigationControllerDelegate method -navigationController:willShowViewController:animated:. You will implement this in view controller A and view controller B. In A you will set -hidesBackButton: to YES and alternatively to NO in view controller B.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
viewController.hidesBackButton = YES;
}
You can also use following sample code;
- (void) viewWillAppear:(BOOL)animated{
self.navigationItem.hidesBackButton = YES;
}
- (void) viewWillDisappear:(BOOL)animated{
self.navigationItem.hidesBackButton = NO;
}
If your view heirarchy is really such that View B should not show a back button but View C should, then the simplest way to get around this is to refactor your heirarchy. I'm thinking of the following alternative:
View A calls presentModalViewController:animated: on View B*, a UINavigationController whose view property is View B. View B* pushes View C onto its navigation stack in response to an event (or otherwise) from View B. If you need to jump back to
View A quickly then call dismissModalViewControllerAnimated: on View A. If you want to keep the state of View B* and C in memory then you could also keep another pointer to View B* somewhere so it doesn't go away when dismissed.

Strange UIKit bug, table view row stays selected

I'm facing what appears to be a UIKit bug, and it takes the combination of two less commonly used features to reproduce it, so please bear with me here.
I have quite the common view hierarchy:
UITabBarController -> UINavigationController -> UITableViewController
and the table view controller pushes another table view controller onto the navigation controller's stack when a row is selected. There's absolutely nothing special or fancy in the code here.
However, the second UITableViewController, the "detail view controller" if you will, does two things:
It sets hidesBottomBarWhenPushed to YES in its init method, so the tab bar is hidden when this controller is pushed:
- (id)initWithStyle:(UITableViewStyle)style {
if(self = [super initWithStyle:style]) {
self.hidesBottomBarWhenPushed = YES;
}
return self;
}
It calls setToolbarHidden:NO animated:YES and setToolbarHidden:YES animated:YES on self.navigationController in viewWillAppear: and viewWillDisappear: respectively, causing the UIToolbar provided by UINavigationController to be displayed and hidden with animations:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setToolbarHidden:YES animated:YES];
}
Now, if the second UITableViewController was pushed by selecting the row at the bottom of the screen (it doesn't have to be the last row) in the first controller, this row does not automatically get deselected when the user immediately or eventually returns to the first controller.
Further, the row cannot be deselected by calling deselectRowAtIndexPath:animated: on self.tableView in viewWillAppear: or viewDidAppear: in the first controller.
I'm guessing this is a bug in UITableViewController's drawing code which of course only draws visible rows, but unfortunately fails to determine correctly if the bottommost row will be visible in this case.
I failed to find anything on this on Google or OpenRadar, and was wondering if anyone else on SO had this problem or knew a solution/workaround.
I had this same exact problem, (though I am not using a toolbar). My solution was to deselect the row in didSelectRowAtIndexPath after pushing my second view controller.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RunViewController *runViewController = [[RunViewController alloc] initWithNibName:#"RunViewController" bundle:nil];
runViewController.managedObjectContext = self.managedObjectContext;
runViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:runViewController animated:YES];
[runViewController release];
//deslect "stuck" row
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
}
If I remember well I think I had same/similar problem once, but my solution was rather rough:
- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// ...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

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];
}

"Hide" the Tab Bar When Pushing a View

The New York Times iPhone application has a Tab Bar with five tab bar items. When you select the Latest tab, the app shows the title and abstract/summary in a UITableView. When you select an individual story to read, the Tab Bar disappears and is replaced with a header and footer that appears/disappears depending on the state of the app. How does the app "hide" the tab bar?
Thanks!
Implement this piece of code in the class where you want to hide the Tab Bar.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
self.hidesBottomBarWhenPushed = YES;
return self;
}
All the best.
The view controller that is being pushed onto the navigation controller stack has its hidesBottomBarWhenPushed parameter set to yes. The code would look something like this in the table view's -didSelectRowAtIndexPath.
NSDictionary *newsItem = [newsItems objectAtIndex:[indexPath row]];
NewsDetailViewController *controller = [[NewsDetailViewController alloc] init];
[controller setHidesBottomBarWhenPushed:YES];
[controller setNewsItem:newsItem];
[[self navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
Take a look at the documentation for hidesBottomBarWhenPushed.
p.s. You'll probably get more visibility on this question if you add the tag 'iphone' to it.
I have a view that needs to optionally (depending on some other state) show the navigation controller toolbar. This is the solution I used to show & hide the toolbar (with animation) when the view appears & disappears via navigation. It sounds like what you might be after.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Show the nav controller toolbar if needed
if (someBool)
[self.navigationController setToolbarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Hide the nav controller toolbar (if visible)
[self.navigationController setToolbarHidden:YES animated:animated];
}