Keeping UINavigationController's navigationBar hidden after UISearchDisplayController selection

后端 未结 5 2045
孤独总比滥情好
孤独总比滥情好 2021-02-04 05:16

I have a UISearchDisplayController setup with a UITableViewController which is nested inside a UINavigationController. When a selection o

相关标签:
5条回答
  • 2021-02-04 05:53

    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.

    0 讨论(0)
  • 2021-02-04 05:54
    - (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 讨论(0)
  • 2021-02-04 05:55

    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!

    0 讨论(0)
  • 2021-02-04 06:03

    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];
    }
    
    0 讨论(0)
  • 2021-02-04 06:04

    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.

    0 讨论(0)
提交回复
热议问题