UISearchDisplayController隐藏导航栏 [英] UISearchDisplayController hiding navigation bar

查看:136
本文介绍了UISearchDisplayController隐藏导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一种奇怪的情况。我在导航栏中放了一个搜索栏,并将UISearchDisplayController与搜索栏相关联。现在,搜索显示控制器倾向于在用户点击搜索栏时隐藏导航栏(因此,也隐藏搜索栏)。为了解决这个问题,我将UISearchDisplayController子类化并实现了以下代码: -

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.

但是,假设我在视图控制器A上进行搜索,然后单击搜索结果,然后将视图控制器B推送到导航堆栈。现在,如果我弹出视图控制器B并返回A,那么我的导航栏就会消失。看起来搜索显示控制器处于活动状态,因此它隐藏了导航栏。

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.

如果我使搜索显示控制器处于非活动状态,然后按下视图控制器B然后弹出它,则会出现导航栏。

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

那么,当我从导航堆栈中弹出视图控制器B时,我的搜索显示控制器是否可以保持活动状态并且导航栏不会消失?

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 ?

我的目标是iOS6

(这是一个很长的代码,所以不知道我应该在这里发布什么)。

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

推荐答案

好的,以防万一,如果有人面临这种​​情况。我针对上述情况实施了一个解决方案。

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

问题是当我从导航堆栈中弹出视图控制器B时,searchDisplayController在视图控制器A中仍处于活动状态现在,searchDisplayController假定搜索栏应始终位于导航栏(AFAIK)下方。因此,当再次显示视图控制器A时,它没有显示导航栏。为了解决这个问题,我在视图控制器A的viewWillLayoutSubviews函数中编写了以下代码。

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

上面提供了一个动画,以便当用户弹出视图控制器B时,视图控制器A显示其搜索栏已激活(如果用户之前曾尝试搜索任何内容,然后再查看控制器B)。它不是一个非常平滑的过渡,但它可以工作:) ....

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 :) ....

注意: - 不要在 viewDidLoad中使用上面的代码 viewDidAppear 函数,因为它可能会提供不良动画。

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

这篇关于UISearchDisplayController隐藏导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆