使用 hidesBottomBarWhenPushed 时,我希望当我推送另一个视图时标签栏重新出现

发布于 2024-11-09 09:24:44 字数 395 浏览 0 评论 0原文

我有一个导航控制器。对于其中一个视图,我想隐藏底部选项卡栏,因此它可以获得最大可能的屏幕空间。为此,我有:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES; // To hide the tab bar
    }
    return self;
}

但是对于我推入堆栈的下一个视图,我希望标签栏重新出现。有办法做到这一点吗?

I have a navigation controller. For one of the views i want to hide the bottom tab bar, so it gets the max possible screen real estate. To do this, i have:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES; // To hide the tab bar
    }
    return self;
}

But for the next view that i push on the stack, i want the tab bar to reappear. Is there a way to do this?

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

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

发布评论

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

评论(6

黄昏下泛黄的笔记 2024-11-16 09:24:44

从 iOS5 开始,有一种非常简单的方法可以实现这一点。它本质上与 Deepak 的方法相同,但动画中没有任何伪影 - 一切看起来都符合预期。

在初始化时,

self.hidesBottomBarWhenPushed = YES;

按照上面的设置进行设置。当需要将新控制器推送到堆栈上时,非常简单:

self.hidesBottomBarWhenPushed = NO;

UIViewController *controller = [[[BBListingController alloc] init] autorelease];
[self.navigationController pushViewController:controller];

self.hidesBottomBarWhenPushed = YES;

在推送控制器后将值重置为 YES 很重要,以便在用户点击“后退”按钮并且视图出现时重新隐藏栏回到视野中。

As of iOS5, there's a very easy means of accomplishing this. It's essentially the same method as Deepak, but there aren't any artifacts with the animation - everything looks as expected.

On init, set

self.hidesBottomBarWhenPushed = YES;

just as you have above. When it's time to push the new controller on the stack, it's as simple as:

self.hidesBottomBarWhenPushed = NO;

UIViewController *controller = [[[BBListingController alloc] init] autorelease];
[self.navigationController pushViewController:controller];

self.hidesBottomBarWhenPushed = YES;

It's important to reset the value to YES after the controller has been pushed in order to re-hide the bar when the user taps the Back button and the view comes back into view.

墨落成白 2024-11-16 09:24:44

我已经这样解决了这个问题:

几乎所有我的 ViewController 都是 BaseViewController 的子级。

因此,例如:

class BaseVC: UIViewController {
    final override var hidesBottomBarWhenPushed: Bool {
        get {
            if navigationController?.viewControllers.last == self {
                return prefersBottomBarHidden ?? super.hidesBottomBarWhenPushed
            } else {
                return false
            }
        } set {
            super.hidesBottomBarWhenPushed = newValue
        }
   }
   private(set) var prefersBottomBarHidden: Bool?
}

只需在 ViewController 中覆盖变量“prefersBottomBarHidden”,其中应隐藏 BottomBar:

override var prefersBottomBarHidden: Bool? { return true }

I'm have solved this problem like that:

Almost all my ViewControllers are children of BaseViewController.

So, example:

class BaseVC: UIViewController {
    final override var hidesBottomBarWhenPushed: Bool {
        get {
            if navigationController?.viewControllers.last == self {
                return prefersBottomBarHidden ?? super.hidesBottomBarWhenPushed
            } else {
                return false
            }
        } set {
            super.hidesBottomBarWhenPushed = newValue
        }
   }
   private(set) var prefersBottomBarHidden: Bool?
}

Just override variable "prefersBottomBarHidden" in ViewController where BottomBar should be hidden:

override var prefersBottomBarHidden: Bool? { return true }
爱给你人给你 2024-11-16 09:24:44

自从提出这个问题以来已经有一段时间了,但是这些答案都没有解决使用故事板转场的问题。事实证明这很简单:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "MyViewControllerIdentifier" {

        // Hide the tabbar during this segue
        hidesBottomBarWhenPushed = true

        // Restore the tabbar when it's popped in the future
        DispatchQueue.main.async { self.hidesBottomBarWhenPushed = false }

    }
}

It's been a while since this question was asked, but none of these answers address using Storyboard segues. It turns out to be pretty easy:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "MyViewControllerIdentifier" {

        // Hide the tabbar during this segue
        hidesBottomBarWhenPushed = true

        // Restore the tabbar when it's popped in the future
        DispatchQueue.main.async { self.hidesBottomBarWhenPushed = false }

    }
}
草莓味的萝莉 2024-11-16 09:24:44

人们可以让它重新出现,但这会导致动画不正确。页面位于左侧,底部栏位于右侧。所以这可能不是您想要的行为。但在同一个控制器中,在推入下一个视图控制器之前执行 self.hidesBottomBarWhenPushed = NO;

One can make it reappear but it will result in an incorrect animation. Page comes in left and the bottom bar right. So it is probably not the behavior you want. But in the same controller, do self.hidesBottomBarWhenPushed = NO; before pushing the next view controller in.

颜漓半夏 2024-11-16 09:24:44

案例一
要在某个 UIVIewController 中隐藏 UITabbarController,例如在调用 self.performSegueWithIdentifier("Identifier", sender: self) 时,必须在此之前设置 self.hidesBottomBarWhenPushed = true< /代码> 标志。在 self.hidesBottomBarWhenPushed = false 标志之后。但我们必须明白,通过一个 UIViewController,UITabbarController 将重新出现,如果您需要将 UITabbarController 与单个 UIViewControler 一起使用,它将不会产生正确的结果。

在 FirstItemViewController 中

 @IBAction func pushToControllerAction(sender: AnyObject) {
        self.hidesBottomBarWhenPushed = true
        self.performSegueWithIdentifier("nextController", 发件人: self)
        self.hidesBottomBarWhenPushed = false
    }

“在此处输入图像描述”"

情况二
要在某个 UIVIewController 中隐藏 UITabbarController,然后弹出 UITabbarController,有必要在调用 self.performSegueWithIdentifier("nextController", sender: self) 时设置 self.hidesBottomBarWhenPushed = true 在该方法之前。另外,方法中的 willMoveToParentViewController(parent: UIViewController?) 应按照代码示例中所示进行配置。

在第一个 UIViewController“FirstItemViewController”

 @IBAction func pushToControllerAction(sender: AnyObject) {
     self.hidesBottomBarWhenPushed = true
     self.performSegueWithIdentifier("nextController", 发件人: self)
 }

在下一个 UIViewController“ExampleViewController”中`

 override func willMoveToParentViewController(parent: UIViewController?) {
         如果父 == nil {
             var viewControllers = self.navigationController!.viewControllers
             if ((viewControllers[viewControllers.count - 2]).isKindOfClass(FirstItemViewController.self)) {
                 (viewControllers[viewControllers.count - 2] as!FirstItemViewController).hidesBottomBarWhenPushed = false
             }
         }
 }

Swift 3 代码中:

let viewControllers = self.navigationController!.viewControllers
                if ((viewControllers[viewControllers.count - 2]) is (FirstItemViewController)) {
                    (viewControllers[viewControllers.count - 2] as! FirstItemViewController).hidesBottomBarWhenPushed = false
                }

在此处输入图像描述

测试项目

Case one:
To hide UITabbarController in a cetain UIVIewController, for example while calling self.performSegueWithIdentifier("Identifier", sender: self), it is necesssary prior to to that, set self.hidesBottomBarWhenPushed = true flag. And after self.hidesBottomBarWhenPushed = false flag. But we have to understad that through one UIViewController, UITabbarController will re-appear and, in case if you need to use UITabbarController with single UIViewControler, it wont yield right result.

in the FirstItemViewController

    @IBAction func pushToControllerAction(sender: AnyObject) {
        self.hidesBottomBarWhenPushed = true
        self.performSegueWithIdentifier("nextController", sender: self)
        self.hidesBottomBarWhenPushed = false
    }

enter image description here

Case Two:
To hide UITabbarController in a certain UIVIewController, after which a UITabbarController should be popped, it is necessary, for example, while calling self.performSegueWithIdentifier("nextController", sender: self) , to set self.hidesBottomBarWhenPushed = true before the method. Alse willMoveToParentViewController(parent: UIViewController?) in the method should be configured as it shown in the code example.

in the first UIViewController "FirstItemViewController"

 @IBAction func pushToControllerAction(sender: AnyObject) {
     self.hidesBottomBarWhenPushed = true
     self.performSegueWithIdentifier("nextController", sender: self)
 }

in the next UIViewController "ExampleViewController"`

 override func willMoveToParentViewController(parent: UIViewController?) {
         if parent == nil {
             var viewControllers = self.navigationController!.viewControllers
             if ((viewControllers[viewControllers.count - 2]).isKindOfClass(FirstItemViewController.self)) {
                 (viewControllers[viewControllers.count - 2] as! FirstItemViewController).hidesBottomBarWhenPushed = false
             }
         }
 }

Swift 3 code:

let viewControllers = self.navigationController!.viewControllers
                if ((viewControllers[viewControllers.count - 2]) is (FirstItemViewController)) {
                    (viewControllers[viewControllers.count - 2] as! FirstItemViewController).hidesBottomBarWhenPushed = false
                }

enter image description here

Test project

凉世弥音 2024-11-16 09:24:44

在根视图控制器“A”(显示 tabBar)中,当需要显示另一个不需要 tabBar 的视图控制器“B”时:

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 a root view controller "A" (which is showing the tabBar), when it comes time to show another view controller "B" where no tabBar is 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 show a third view controller 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文