【问题标题】:Navigation Bar on only one view in my whole app我的整个应用程序中只有一个视图上的导航栏
【发布时间】:2019-05-07 09:37:39
【问题描述】:

我的应用中有几个视图,我只想要其中一个上的 navigationbar....只有 2 次观看)

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
    super.viewWillAppear(animated)
}

override func viewWillDisappear(_ animated: Bool) {
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
    super.viewWillDisappear(animated)
}

它运行良好 - 但是,应用程序变得更加复杂 - 我有这些观点

lazy var orderedViewControllers: [UIViewController] = {
    return [self.newVc(viewController: "pageOne"),
            self.newVc(viewController: "pageTwo"),
            self.newVc(viewController: "pageThree"),
            self.newVc(viewController: "pageFour"),
            self.newVc(viewController: "activate")
    ]
}()

即使我为每个视图创建了一个自定义视图控制器,也没有应用此代码。

我认为这样做的方法是将顶部的代码块放在每个视图中,但它不适用于底部的块。本质上,我的问题是如何使用NavigationController 仅在一个view 上创建一个栏。

【问题讨论】:

  • 嗨大卫,你能解释一下,你需要哪些功能?
  • 是的。我有几种看法。我的主视图有一个按钮可以转到设置页面。这个设置页面是有一个顶部导航栏,带有一个返回按钮可以返回到主页面。我用 UINavigationView 实现了这个功能——但是,我从主控制器导航到的任何页面现在都有这个顶部导航栏。我希望设置页面有导航栏。
  • 你检查了这个链接吗:stackoverflow.com/questions/845583/…
  • 是的,就像我说的那样,我设法让它在一页上工作,但在使用轮播视图时却不行。
  • 您可以添加您有问题的视图的图像吗?

标签: ios swift uinavigationcontroller uinavigationbar uistoryboard


【解决方案1】:

一种选择:使用“基本视图控制器”类来处理隐藏/显示导航栏,并使“页面”成为“基本”类的子类。

import UIKit

class BaseViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
    }

}

class ViewController: UIViewController {

    // has buttons with
    //    Show (e.g. push)
    // segues to Settings, First, Second, Third view controllers

}

class SettingsViewController: UIViewController {

    // Settings VC is a normal UIViewController, because
    // we *want* the NavBar to remain visible

}

class FirstViewController: BaseViewController {
    @IBAction func backTapped(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
}

class SecondViewController: BaseViewController {
    @IBAction func backTapped(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
}

class ThirdViewController: BaseViewController {
    @IBAction func backTapped(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
}

【讨论】:

    【解决方案2】:

    你可以使用UINavigationControllerDelegate这个方法

    optional func navigationController(_ navigationController: UINavigationController, 
                              willShow viewController: UIViewController, 
                              animated: Bool){
         if viewController == self."desired view controller" {
            self.isNavigationBarHidden = true
        }else{
            self.isNavigationBarHidden = false 
        }
    }
    

    【讨论】:

      【解决方案3】:

      感谢大家的支持。我通过执行以下操作解决了我的问题:

      1. 我将唯一希望在导航控制器视图中具有导航栏的视图控制器放入嵌入菜单。
      2. 我添加了一个自定义后退按钮。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-25
        相关资源
        最近更新 更多