在选择 UISearchDisplayController 后保持 UINavigationController 的导航栏隐藏

发布于 2024-08-23 14:03:57 字数 745 浏览 5 评论 0原文

我有一个 UISearchDisplayController 设置,其中包含一个 UITableViewController ,它嵌套在 UINavigationController 内。当选择一个单元格时,UITableView 的didSelectRowAtIndexPath 方法被触发,该方法将一个新视图推送到父导航控制器。这个新视图应该在入口处隐藏导航栏。

[[self navigationController] setNavigationBarHidden:YES animated:NO];

我在 didSelectRowAtIndexPath 方法中使用这一行来隐藏导航栏。当不使用搜索控制器选择一行时,此功能可以正常工作,但在选择搜索结果时会被覆盖。似乎 UISearchDisplayController 会在选择行后的某个时间取消隐藏导航栏。

如果我将 setNavigationBarHidden 调用移至目标视图的 viewWillAppear 方法中,结果是相似的。我可以通过将 hide 调用放在 viewDidAppear 中来使其工作,但这会产生非常尴尬的过渡效果,让人感觉跳跃和不合适。我想在新视图滑到屏幕上之前使导航栏已经隐藏。

有谁知道导航栏的取消隐藏发生在哪里,和/或我可以通过什么方式覆盖此行为?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巴黎盛开的樱花 2024-08-30 14:03:58
- (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];
}
- (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];
}
潦草背影 2024-08-30 14:03:57

这可能不是最优雅的解决方案,但我相信它完全符合您的要求。我遇到了类似的问题,我的解决方案是有一个隐藏导航栏的方法,在延迟 0 秒后调用该方法,如下所示。

调用的方法是:

-(void) hideNavBar {
    if (self.navigationController.navigationBar.hidden == NO)
    {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}

然后在 viewDidLoad 方法中,我有以下内容:

[self performSelector:@selector(hideNavBar) withObject:nil afterDelay:0.0];

这可以工作并瞬间删除导航栏。如果您想要动画或在延迟后将其删除,您可以修改延迟时间。我尝试了 [self hideNavBar] 但这根本不起作用,所以坚持我上面的内容。

希望这有帮助,如果有人有更优雅的解决方案,我很感兴趣!

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!

萌︼了一个春 2024-08-30 14:03:57

好吧,这困扰了我几个小时,但我终于让它工作了!问题似乎是 UISearchDisplayController 跟踪它是否隐藏了导航栏,如果隐藏了,它会在视图关闭后恢复它。这就是为什么在上面的许多答案中,当推送新视图时,您会看到栏动画的尾部隐藏自身。然而,通过欺骗搜索显示控制器,我们可以改变这种行为。

首先:子类化 UISearchDisplayController

按照有关如何防止隐藏导航控制器的答案,可以在此处,我修改了代码,以保持导航栏隐藏:

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

请注意,我们在调用超级 setActive 函数之前隐藏了导航栏。这似乎可以防止超级类尝试隐藏导航栏,从而避免在选择项目后尝试恢复它。现在,当控制器处于活动状态时,该栏将像平常一样隐藏。另请注意,当 searchBar 放弃第一响应者时,我们会恢复导航栏。如果我们取消控制器,这将恢复栏。

第二:退出时隐藏导航栏

如果我们隐藏视图中的导航栏会消失,那么它会被隐藏:

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

第三:返回时隐藏导航栏

现在唯一的问题是如果我们从过滤后的表格视图中选择一行,当我们返回时,导航栏将可见。为了解决这个问题,我们需要在视图中进行检查:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if(self.isFiltered){
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    }

}

虽然这感觉像是一个巨大的黑客攻击,但它确实达到了目的,而且我看不出有更好的方法来做到这一点。

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.

微凉 2024-08-30 14:03:57

遇到了同样的问题,设法通过这个丑陋的黑客让它顺利工作:

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

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];
}
失去的东西太少 2024-08-30 14:03:57

我遇到了同样的问题:我的视图默认隐藏导航栏,这是保持隐藏状态的方法:

-(void) viewWillLayoutSubviews{
  if (self.navigationController.navigationBar.hidden == NO)
  {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
  }
}

这样,即使使用搜索栏后,导航栏也不会出现。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文