导航栏没有显示在TableView上

时间:2017-11-17 02:47:24

标签: ios uitableview swift3 uinavigationcontroller uinavigationbar

我无法解释为什么此代码无效。出于某种原因,导航栏是半透明的,并且tableView的内容在其上滚动。

这是Button的代码

 let outlineViewController = OutlineTableviewController()
        outlineViewController.pdfOutlineRoot = pdfoutline
        outlineViewController.delegate = self

        let nav = UINavigationController(rootViewController: outlineViewController)
        self.present(nav, animated: true, completion:nil)

这是tableView的代码

super.viewDidLoad()

    let newBackButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(dismissView))

    self.navigationItem.rightBarButtonItem = newBackButton

    navigationController?.navigationBar.backgroundColor = UIColor(red:0.00, green:0.51, blue:0.78, alpha:1.0)


   self.tableView.dataSource = self
   self.tableView.delegate = self

   tableView?.register(UINib(nibName: "OutlineTableViewCell", bundle: nil),
                        forCellReuseIdentifier: "OutlineTableViewCell")
}

结果如下: 背景颜色未显示,表格位于导航栏下方。

我错过了什么?

enter image description here

2 个答案:

答案 0 :(得分:0)

感谢trungduc的回复。

花了好几个小时试图找出问题所在,我终于能够通过改变根视图中的颜色来解决这个问题。

        let nav = UINavigationController(rootViewController: oulineViewController)
        nav.navigationBar.backgroundColor = UIColor(red:0.00, green:0.51, blue:0.78, alpha:1.0)
        self.present(nav, animated: true, completion:nil)

答案 1 :(得分:0)

你在问题​​中提到了一个神奇的词:“半透明。”

我遇到了同样的问题:导航栏显示在Storyboard的根视图中,但是在运行模拟器时 - 视图顶部没有导航栏。这解决了它:

导航控制器>导航栏> UNCHECK半透明(默认选中)。这做了两件事:

  1. 我的导航栏会显示所有后续视图。
  2. 最顶层的子视图(在你的情况下是一个tableview)现在是Y = 0,而不是Y = 64。
  3. enter image description here

相关问题