如果我只想隐藏一种控制器的选项卡栏,如何使用 hidesBottomBarWhenPushed ?

发布于 2024-11-14 09:59:31 字数 129 浏览 3 评论 0原文

我在使用 hidesBottomBarWhenPushed 时遇到了麻烦... 我将按顺序将三个控制器 - A、B 和 C - 推入导航控制器,并且我想在显示 B 时隐藏底部选项卡栏。(A 是选项卡栏控制器之一)

有人有想法吗?

I have gotten in trouble while using hidesBottomBarWhenPushed...
I will push three controllers – A, B, and C – into navigationcontroller in order, and I would like to hide bottom tab bar when B is shown.(and A is one of the tabbar controllers)

Does any one have ideas?

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

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

发布评论

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

评论(3

以歌曲疗慰 2024-11-21 09:59:31

在视图控制器 A(位于 tabBar 上)中,当需要呈现 B(不需要 tabBar)时:

self.hidesBottomBarWhenPushed = YES; // hide the tabBar when pushing B
[self.navigationController pushViewController:viewController_B animated:YES];
self.hidesBottomBarWhenPushed = NO; // for when coming Back to A

在视图控制器 B 中,当需要呈现 C(再次需要 tabBar)时:

self.hidesBottomBarWhenPushed = NO; // show the tabbar when pushing C
[self.navigationController pushViewController:viewController_C animated:YES];
self.hidesBottomBarWhenPushed = YES; // for when coming Back to B

In view controller A (which is on the tabBar), when it comes time to present B (no tabBar wanted):

self.hidesBottomBarWhenPushed = YES; // hide the tabBar when pushing B
[self.navigationController pushViewController:viewController_B animated:YES];
self.hidesBottomBarWhenPushed = NO; // for when coming Back to A

In view controller B, when it comes time to present C (tabBar wanted again):

self.hidesBottomBarWhenPushed = NO; // show the tabbar when pushing C
[self.navigationController pushViewController:viewController_C animated:YES];
self.hidesBottomBarWhenPushed = YES; // for when coming Back to B
╭ゆ眷念 2024-11-21 09:59:31

我发现有时这已经太晚了,而不是在 viewDidLoad 中设置它。在 init 中设置它或覆盖 hidesBottomBarWhenPushed 以便对于没有底部工具栏的视图返回 YES。

Instead of setting it in viewDidLoad, I have found that sometimes this is too late. Set it in init or override hidesBottomBarWhenPushed to return YES for views with no bottom toolbar.

只为一人 2024-11-21 09:59:31

来自 hidesBottomBarWhenPushed 文档:

如果是,底部栏将保持隐藏状态,直到视图控制器被打开。
从堆栈中弹出。

这意味着,如果您不一定知道视图控制器的推送顺序,则需要将堆栈中的所有视图控制器的 hidesBottomBarWhenPushed 设置为 false(topViewController 除外)。

因此,

  1. 在推送新视图控制器之前,我会根据需要设置其 hidesBottomBarWhenPushed 属性,
  2. 然后在推送之前设置 self.hidesBottomBarWhenPushed 以确保整个堆栈,直到下一个堆栈
  3. 在弹出之前将其属性设置为 false,此时我检查是否tabBar 应该显示或不显示,并更新其 hidesBottomBarWhenPushed

这是 1 和 2 的一些代码)

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    self.hidesBottomBarWhenPushed = false

    if (segue.identifier == "MyViewControllerWhoHidesTabBar") {
      let viewController: MyViewControllerWhoShowsTabBar = segue.destinationViewController as! MyViewControllerWhoShowsTabBar
      viewController.hidesBottomBarWhenPushed = true
    }
       // rest of implementation....
 }

3)我已将后退按钮操作重写为

  func backButtonClick(sender:UIButton!) {
    let viewControllers = self.navigationController!.viewControllers
    if let vc = viewControllers[viewControllers.count-2] as? MyViewController {
      if vc.isKindOfPageYouDontWannaShowTheTabBar() == true {
        vc.hidesBottomBarWhenPushed = true
      }
    }
    navigationController?.popViewControllerAnimated(true)
  }

From hidesBottomBarWhenPushed documentation:

If YES, the bottom bar remains hidden until the view controller is
popped from the stack.

This means that if you don't necessarily know the order the View Controllers will be pushed, you'll need all the view controllers from the stack to have its hidesBottomBarWhenPushed set to false except for the topViewController.

So what I do

  1. before pushing the new View Controller I set its hidesBottomBarWhenPushed property as desired
  2. also before pushing the I set self.hidesBottomBarWhenPushed to ensure the whole stack until the next one will have its property set to false
  3. before popping, that's when I check if the tabBar should be displayed or not, and update its hidesBottomBarWhenPushed

Here's some code for 1 and 2)

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    self.hidesBottomBarWhenPushed = false

    if (segue.identifier == "MyViewControllerWhoHidesTabBar") {
      let viewController: MyViewControllerWhoShowsTabBar = segue.destinationViewController as! MyViewControllerWhoShowsTabBar
      viewController.hidesBottomBarWhenPushed = true
    }
       // rest of implementation....
 }

3) I've overriden the back button action to

  func backButtonClick(sender:UIButton!) {
    let viewControllers = self.navigationController!.viewControllers
    if let vc = viewControllers[viewControllers.count-2] as? MyViewController {
      if vc.isKindOfPageYouDontWannaShowTheTabBar() == true {
        vc.hidesBottomBarWhenPushed = true
      }
    }
    navigationController?.popViewControllerAnimated(true)
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文