Sausage Machine Sausage Machine - 1 year ago 199
iOS Question

How to make navigation bar transparent in iOS 10

I have the following code to make the navigation bar transparent but while still displaying the back button, this works on all versions of iOS but its stopped working with the iOS 10 beta

navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationBar.shadowImage = UIImage()
navigationBar.isTranslucent = true

Has something changed with iOS 10 in this area?

Note its not possible to use navigationBar.isHidden as this would result in the navigation bar back button and title etc. disappearing also.

Answer Source

I don't know what has changed in iOS 10 to stop the previous code from working, but to fix it I created a transparent image (it only needs to be one pixel in dimension) and used the following code to make the navigation bar transparent (but still showing the back navigation button).

    let transparentPixel = UIImage(named: "TransparentPixel")
    navigationBar.setBackgroundImage(transparentPixel, for: UIBarMetrics.default)
    navigationBar.shadowImage = transparentPixel
    navigationBar.backgroundColor = UIColor.clear()
    navigationBar.isTranslucent = true

Incidentally, if you want to change the color of the navigation bar, you can use the same principle:

    let redPixel = UIImage(named: "RedPixel")
    navigationBar.setBackgroundImage(redPixel, for: UIBarMetrics.default)
    navigationBar.shadowImage = redPixel
    navigationBar.isTranslucent = false
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download