UISearchDisplayController hiding navigation bar

≯℡__Kan透↙ 提交于 2019-11-30 19:28:52

问题


I am seeing a weird situation. I have put a search bar in the navigation bar and have linked a UISearchDisplayController with the search bar. Now, the search display controller tends to hide the navigation bar when the user clicks on the search bar (therefore, hiding the search bar also). In order to counter that, I subclassed UISearchDisplayController and implemented the following code :-

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
  [super setActive: visible animated: animated];
  [self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
}

Now, this fixed the original problem. I am able to search and navigate to other controllers.

However, lets say that I do a search on view controller A and then click on a search result which then pushes view controller B on the navigation stack. Now, if I pop the view controller B and return back to A, then my navigation bar disappears. It looks like that the search display controller is active and so it hides the navigation bar.

If I make the search display controller inactive and then push view controller B and then pop it, then the navigation bar appears.

So, is there any way that my search display controller can remain active and the navigation bar does not disappear when I pop view controller B from navigation stack ?

And I am targeting iOS6

(It is a very long code so not sure what I should post here).


回答1:


okay and just in case, if somebody faces this kind of situation. I implemented a work around for the above situation.

The problem was that the when I popped view controller B from the navigation stack, the searchDisplayController was still active in view controller A. Now, the searchDisplayController assumes that the search bar should always be below the navigation bar (AFAIK). Therefore, it did not displayed the navigation bar when view controller A was again displayed. To fix that, I wrote the following code in the viewWillLayoutSubviews function of view controller A.

-(void)viewWillLayoutSubviews
{
    if(self.searchDisplayController.isActive)
    {
        [UIView animateWithDuration:0.001 delay:0.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            [self.navigationController setNavigationBarHidden:NO animated:NO];
        }completion:nil];
    }
    [super viewWillLayoutSubviews];   
}

The above provides a animation so that when the user pops view controller B, the view controller A shows its search bar activated (if the user had earlier tried to search for anything before going to view controller B). It is not a very smooth transition but it works :) ....

Note :- Do not use the above code in viewDidLoad or viewDidAppear functions as it might provide an undesirable animation.




回答2:


-(void)viewDidLayoutSubviews{
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

it will not hide navigation bar.




回答3:


In case anyone suffer from this issue.. Here comes my solution.

-(void) viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  // check if searchDisplayController still active..
  if ([searchDisplayController isActive]) {
    [searchDisplayController setActive:NO];
  }
}



回答4:


My fix is working

  override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        DispatchQueue.main.async {
            self.navigationController?.setNavigationBarHidden(true, animated: false)
        }
}


来源:https://stackoverflow.com/questions/18538535/uisearchdisplaycontroller-hiding-navigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!