开发者

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 开发者_Go百科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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜