在Swift,导航栏消失,再也没有回来

In Swift, the navigation Bar dissappear and never comes back

我正在尝试通过按一个按钮从当前控制器推送一个新的视图控制器。但是新控制器一出现,顶部的导航栏就消失了,我尝试了很多方法,但似乎无法恢复。

我在不使用 Interface Builder 的情况下以编程方式编写所有代码。

我已经尝试了下面的代码列表,但其中 none 行得通。

  override func viewDidLoad() {
    super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(false, animated: false)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "backimg"), style: .plain, target: self, action: #selector(backTapped))

    let webV:UIWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    webV.loadRequest(NSURLRequest(url: NSURL(string: "https://*****************.com")! as URL) as URLRequest)
    webV.delegate = self;
    self.view.addSubview(webV)
    self.navigationController?.navigationBar.isHidden = false
    navigationController?.isNavigationBarHidden = false
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    self.navigationController?.navigationBar.isHidden = false
    self.navigationController?.setNavigationBarHidden(false, animated: false)
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    self.navigationController?.navigationBar.isHidden = false
    self.navigationController?.setNavigationBarHidden(false, animated: false)
}

@objc func backTapped(_ sender: Any){
    self.dismiss(animated: true, completion: nil)

}

我正在尝试从按钮的目标函数中推送视图控制器,如下所示:

  @objc func parkingTimerTapped(_ sender: Any) {
    let pp = ParkingModeScheduleView()

    self.present(pp, animated: true, completion: nil)
    print("Parking Timer Tapped")
 }

我也已经尝试使用以下命令推送视图控制器:

  self.navigationController?.pushViewController(pp, animated: true)

我是不是做错了什么或遗漏了什么?

使用 self.present(pp, animated: true, completion: nil) 将您的新视图控制器显示为模式,因此它根本不是导航堆栈的一部分,这就是没有导航栏的原因。

我建议尝试使用 self.navigationController?.pushViewController(pp, animated: true),但首先检查 self.navigationController 是否出于某种原因不是 nil。同时从 ParkingModeScheduleView

中删除所有隐藏的导航栏相关方法

您需要像这样在导航控制器中添加您的 parkingModeScheduleview

@objc func parkingTimerTapped(_ sender: Any) {
    let pp = ParkingModeScheduleView()
    let navigation = UINavigationController(rootViewController: pp)
    self.present(navigation, animated: true, completion: nil)
    print("Parking Timer Tapped")
 }