How to make the navigation bar transparent in iOS 10

I have the following code to make the navigation bar transparent, but when showing the back button it works in all versions of iOS, but it stopped working with the beta version of iOS 10

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

Has something changed with iOS 10 in this area?

Please note that it is not possible to use navigationBar.isHidden, as this will cause the back button and the title bar to disappear.

+7
ios swift uinavigationbar
source share
3 answers

I don’t know what has changed in iOS 10 to stop the previous code, but to fix it I created a transparent image (it should be only one pixel in dimension) and used the following code to make the navigation bar transparent (but the reverse is still displayed navigation button).

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

By the way, 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 
+10
source share

@Essence's solution is great!
This is what I use even to create a transparent 1px image by code:

 class MainClass: UIViewController { let transparentPixel = UIImage.imageWithColor(color: UIColor.clear) override func viewWillAppear(_ animated: Bool) { drawCustomNavigationBar() } func drawCustomNavigationBar() { let nav = (self.navigationController?.navigationBar)! nav.setBackgroundImage(transparentPixel, for: UIBarMetrics.default) nav.shadowImage = transparentPixel nav.isTranslucent = true } } extension UIImage { class func imageWithColor(color: UIColor) -> UIImage { let rect = CGRect(origin: CGPoint(x: 0, y:0), size: CGSize(width: 1, height: 1)) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext()! context.setFillColor(color.cgColor) context.fill(rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } } 
+6
source share

Swift 3.x

 self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.backgroundColor = .clear self.navigationController?.navigationBar.isTranslucent = true 
+1
source share

All Articles