简体   繁体   English

导航栏未显示在TableView上方

[英]Navigation bar not showing over TableView

I can't explain why this code is not working. 我无法解释为什么此代码无法正常工作。 For some reason the navigation bar is translucent and the contents of the tableView are scrolling over it. 出于某种原因,导航栏是半透明的,并且tableView的内容正在其上滚动。

here is the code of the Button 这是按钮的代码

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

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

here is the code of the tableView 这是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")
}

and here is the result: the background color is not showing and the table goes under the navigationbar. 结果如下:不显示背景色,表格位于导航栏下方。

What am I missing? 我想念什么?

在此处输入图片说明

Thank you trungduc for your reply. 谢谢trungduc的回复。

after spending hours trying to figure out what the problem was, I finally was able to fix this issue by changing the color in the root view. 在花了几个小时试图找出问题所在之后,我终于能够通过更改根视图中的颜色来解决此问题。

        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)

You mentioned the magic word in your question: "translucent." 您在问题中提到了魔术字:“半透明”。

I had the same problem: the navigation bar was showing on the root view in Storyboard, but when running the Simulator - there was no navigation bar at the top of the views. 我遇到了同样的问题:导航栏显示在Storyboard的根视图上,但是在运行Simulator时,视图顶部没有导航栏。 This solved it: 这解决了它:

Navigation Controller > Navigation Bar > UNCHECK Translucent (it is checked by default). 导航控制器>导航栏>取消半透明(默认选中)。 This did two things: 这做了两件事:

  1. My Navigation Bar shows on all subsequent views. 我的导航栏显示在所有后续视图中。
  2. The topmost subview (a tableview in your case) is now at Y=0, and not Y=64. 现在最顶层的子视图(在您的情况下为表格视图)位于Y = 0,而不是Y = 64。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM