在Swift中,导航栏消失了,永远不会回来

问题描述 投票:3回答:2

我试图通过按下按钮从当前控制器推送一个新的视图控制器。但是一旦出现新的控制器,顶部的导航栏就会消失,我尝试了很多方法,但似乎无法将其取回。

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

我已经尝试了下面的代码列表,但没有一个工作。

  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)

我做错了什么或遗失了什么?

swift uinavigationcontroller uinavigationbar pushviewcontroller
2个回答
2
投票

你需要像这样在UINavigationcontroller中添加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")
 }

1
投票

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

我建议尝试使用self.navigationController?.pushViewController(pp, animated: true),但首先要检查self.navigationController是不是因为某种原因而不是nil。同时从ParkingModeScheduleView中删除所有导航栏隐藏的相关方法

© www.soinside.com 2019 - 2024. All rights reserved.