【问题标题】:Segue Not Showing Navigation BarSegue 不显示导航栏
【发布时间】:2018-06-01 03:45:45
【问题描述】:

我有 2 个ViewControllers 正在展示一个新的ViewController

第一个是在导航控制器中,因此它可以通过 segue 推送按预期工作。

然而,第二个来自没有导航栏的ViewController。我以编程方式呈现这种观点。但是,当提出目的地时,有两个问题......

1) 没有导航栏。 2) 显示的视图从第一个 TableViewCell 下方开始。

func goToLocation() {
    let locationTableVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "locationProfile") as! LocationTableViewController
    locationTableVC.documentId = selectedDocumentId!
    self.present(locationTableVC, animated: true, completion: nil)
}

LocationTableViewController.swift

// MARK: - View Will Appear
override public func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.statusBarStyle = .lightContent

    // Make Nav Bar Translucent and Set title font/color
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.view.backgroundColor = .clear
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: .semibold)]
    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back-arrow-white")

}

Segue 显示从第一个 TableViewCell 下方开始,并且没有导航栏。

我试图像第二个一样重新创建的第一个 segue 看起来像这样......

【问题讨论】:

  • "1)" 很简单,它没有导航控制器,因为您没有来自该视图的导航控制器。您必须推送嵌入在 UINavigationController 中的视图控制器才能拥有一个。如果从其他视图推送时也可以解决“2)”问题。
  • @Daniel 所以如果我不想看到第一个 VC 上显示的导航栏,尽管我只是隐藏它?
  • 你不需要隐藏它,只需要在需要的时候创建一个。看我的回答。

标签: ios swift uinavigationcontroller uinavigationbar segue


【解决方案1】:

使用 UINavigationController 推送您的 UIViewController,如下所示:

func goToLocation() {
    let locationTableVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "locationProfile") as! LocationTableViewController
    locationTableVC.documentId = selectedDocumentId!
    let navigationController = UINavigationController(rootViewController: locationTableVC)
    self.present(navigationController, animated: true, completion: nil)
}

【讨论】:

  • 感谢@Daniel 也解决了 #2。我唯一需要做的就是设置返回/关闭按钮。我是否将其添加到 goToLocation 函数中?或者这会在locationTableVC 中吗?
  • 它将在 locationTableVC 中
  • 这似乎覆盖了另一个后退按钮。因为那时我必须调用一个函数和popViewController,它适用于另一个视图,但适用于来自goToLocation的ViewController
  • 没错,你需要一个 if 来决定是否需要添加那个按钮
  • 100% 否则无法离开 VC
猜你喜欢
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多