Make UINavigationBar transparent
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
Related videos on Youtube
quano
Updated on January 31, 2022Comments
-
quano over 2 years
How do you make a UINavigationBar transparent? Though I want its bar items to remain visible.
-
quano about 14 yearsI 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 almost 13 yearsIm sorry, this isn't correct. You still have to override the drawRect method
-
Yang Meyer over 12 yearsIt 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 over 11 yearsWhy 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 almost 11 yearsIn 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 over 9 yearsAnswering 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 over 9 yearsnavigationController is an optional property so you need to unwrap it. Simply add self.navigationController?.navigationBar and you are good to go
-
Guilherme over 9 yearsI need only one VC to have a transparent navbar. How do I revert to the original style after exiting that VC?
-
dy_ about 9 yearsto 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 about 9 yearsWorks using appeareance as well (iOS7/8), see : gist.github.com/mpycio/ddbdea1adb6b86cf02f6
-
jab about 9 yearsIf 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 over 8 yearsWorks with Appearance proxy in iOS 9
-
Henning about 8 yearsUndoing 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 almost 8 yearsif need a percentage how should be?
-
ghr over 7 yearsFor 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 over 7 yearsSetting
translucent
toNO
works for me in iOS 10.2 -
Mario almost 6 years@Stone Currently struggling with exactly this issue, have you been able to solve it?
-
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 about 4 yearsAwesome! Was just searching for a fix! Works great! Thanks!
-
Sam Soffes about 3 yearsYou'll need
navigationBar.standardAppearance.backgroundEffect = nil
too in newer iOS verisons -
nolanw about 3 yearsThank you! Note that if you're already setting
standardAppearance
viaUIAppearance
, setting thebackgroundColor
/backgroundEffect
/shadowColor
as shown here may not work, becauseUINavigationBarAppearance
properties do not work withUIAppearance
. Instead, you need to make your own instance ofUINavigationBarAppearance
, set the properties as you like, then setnavigationBar.standardAppearance = myNewInstance
. -
Dannie P over 2 yearsThe solution availability is iOS 13+ (navigationItem.standardAppearance)