Make UINavigationBar transparent

140,181

Solution 1

If anybody is wondering how to achieve this in iOS 7+, here's a solution (iOS 6 compatible too)

In Objective-C

[self.navigationBar setBackgroundImage:[UIImage new]
                         forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;

In swift 3 (iOS 10)

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

In swift 2

self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true

Discussion

Setting translucent to YES on the navigation bar does the trick, due to a behavior discussed in the UINavigationBar documentation. I'll report here the relevant fragment:

If you set this property to YES on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image.

Solution 2

In iOS5 you can do this to make the navigation bar transparent:

nav.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

[nav.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault]; 
[img release];

Solution 3

From IOS7 :

self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.view.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];

Solution 4

If you build with the latest beta iOS 13.4 and Xcode 11.4, the accepted answer won't work anymore. I've found another way, maybe it's just a bug in the beta software, but I'm writing it down there, just in case

(swift 5)

import UIKit

class TransparentNavBar :UINavigationBar {
    override func awakeFromNib() {
        super.awakeFromNib()
        self.setBackgroundImage(UIImage(), for: .default)
        self.shadowImage = UIImage()
        self.isTranslucent = true
        self.backgroundColor = .clear
        if #available(iOS 13.0, *) {
            self.standardAppearance.backgroundColor = .clear
            self.standardAppearance.backgroundEffect = .none
            self.standardAppearance.shadowColor = .clear
        }
    }
}

Solution 5

For anyone who wants to do this in Swift 2.x:

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

or Swift 3.x:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
Share:
140,181

Related videos on Youtube

quano
Author by

quano

Updated on January 31, 2022

Comments

  • quano
    quano over 2 years

    How do you make a UINavigationBar transparent? Though I want its bar items to remain visible.

  • quano
    quano about 14 years
    I mean the former. I tried making a category and overriding the drawRect method of UINavigationBar (invoking CGContextClearRect), but that made it completely black. The items were still visible though.
  • Sander
    Sander almost 13 years
    Im sorry, this isn't correct. You still have to override the drawRect method
  • Yang Meyer
    Yang Meyer over 12 years
    It seems that in iOS 5 you must override -drawRect: in a subclass proper, not in a category, and then use this subclass as your navigation bar.
  • Cristi
    Cristi over 11 years
    Why is this wrong? It seems to be working in IOS 6 sim. It isn't working in IOS 5? navigationBar.backgroundColor seems to be undocumented.
  • Robert
    Robert almost 11 years
    In iOS 6 you will also want to remove the navigation bar shadow, otherwise it will look strange. [[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];
  • Johann Burgess
    Johann Burgess over 9 years
    Answering my own comment here; to undo the affect try:[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.shadowImage = nil; self.navigationController.navigationBar.translucent = NO;
  • Daniel Galasko
    Daniel Galasko over 9 years
    navigationController is an optional property so you need to unwrap it. Simply add self.navigationController?.navigationBar and you are good to go
  • Guilherme
    Guilherme over 9 years
    I need only one VC to have a transparent navbar. How do I revert to the original style after exiting that VC?
  • dy_
    dy_ about 9 years
    to achieve this in Swift from within a ViewController, do it like this: self.navigationController?.navigationBar.setBackgroundImage(‌​UIImage(), forBarMetrics: UIBarMetrics.Default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.translucent = true
  • Mahakala
    Mahakala about 9 years
    Works using appeareance as well (iOS7/8), see : gist.github.com/mpycio/ddbdea1adb6b86cf02f6
  • jab
    jab about 9 years
    If anyone is wondering, you can undo the effect in Swift the same way -- in spite of the fact the function takes UIImage! as an argument, sending nil works.
  • user3099609
    user3099609 over 8 years
    Works with Appearance proxy in iOS 9
  • Henning
    Henning about 8 years
    Undoing the effect for me only partially works. Because after I do this, my tableviews all have headers that are too large. App-wide, which is weird. (I only want this effect for one controller that I push on the stack.)
  • jose920405
    jose920405 almost 8 years
    if need a percentage how should be?
  • ghr
    ghr over 7 years
    For me, in iOS9, and as mentioned lower down, remove 1 line (has no effect that I can see) and add another: [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]]; //self.navigationController.navigationBar.shadowImage = [UIImage new]; self.navigationController.navigationBar.translucent = YES;
  • Iulian Onofrei
    Iulian Onofrei over 7 years
    Setting translucent to NO works for me in iOS 10.2
  • Mario
    Mario almost 6 years
    @Stone Currently struggling with exactly this issue, have you been able to solve it?
  • Stone
    Stone almost 6 years
    @Mario unfortunately no. Our workaround was to make the NavigationBar transparent by default and work with a background image. Have a look at this: stackoverflow.com/a/48691715/2307466
  • Georg
    Georg about 4 years
    Awesome! Was just searching for a fix! Works great! Thanks!
  • Sam Soffes
    Sam Soffes about 3 years
    You'll need navigationBar.standardAppearance.backgroundEffect = nil too in newer iOS verisons
  • nolanw
    nolanw about 3 years
    Thank you! Note that if you're already setting standardAppearance via UIAppearance, setting the backgroundColor/backgroundEffect/shadowColor as shown here may not work, because UINavigationBarAppearance properties do not work with UIAppearance. Instead, you need to make your own instance of UINavigationBarAppearance, set the properties as you like, then set navigationBar.standardAppearance = myNewInstance.
  • Dannie P
    Dannie P over 2 years
    The solution availability is iOS 13+ (navigationItem.standardAppearance)