Why is the UINavigationBar turning black?

Problem:

I have a UITableViewController embedded in a UINavigationController . Pressing a cell in the table view switches to another table view controller. In said table view controller, I'd like for the navigation bar to be invisible while still keeping the tab bar items so I added the following to its viewDidLoad() :

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.translucent = true
        self.navigationController?.navigationBar.tintColor = .blackColor() 

For the first UITableViewController, I'd like the navigation bar to be normal so in its viewDidAppear() I did the following:

self.navigationController?.navigationBar.translucent = false

Everything is working fine except during the transition (which I am doing via performSegueWithIdentifier ) the navigation bar on the first view controller disappears into blackness which looks ugly to be honest. Is there any way to prevent/fix this?

Screenshot: 在这里输入图像描述


I had a very similar problem recently. Try setting self.navigationController?.navigationBar.translucent = true in both view controllers and self.edgesForExtendedLayout = UIRectEdgeNone .
Storyboard version: Extended Edges - Under Top Bars


You can animate the translucency of the navigation bar. So in the viewDidLoad for your second UITableViewController , you can write the following:

override func viewDidLoad() {
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.tintColor = .blackColor()

    // Play around with the duration until you find
    // a time interval, you find suitable
    UIView.animateWithDuration(2) {
        self.navigationController?.navigationBar.translucent = true
    }
}

I ran into this again recently, and discovered a way to fix it in the storyboard. If you're using opaque navigation bars, ensure the "Extend Edges" setting for "Under Opaque Bars" is set. In fact, I just set all three of them as shown below:-

在这里输入图像描述

链接地址: http://www.djcxy.com/p/85686.html

上一篇: Android M中的INTERNET权限

下一篇: 为什么UINavigationBar变黑?