Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UINavigationBar background transparent?

First of all, I've seen all the answers at How to make UINavigationBar Transparent in IOS 8? Transparent UINavigationBar and Make UINavigationBar transparent.

They just don't seem to work for me.

My regular view controller (before trying to make the navigation bar transparent) doesn't have any issues:

enter image description here

I'm using (tried both in viewDidLoad and viewWillAppear:):

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

I'm getting this:

enter image description here

Gray status bar background, completely white navigation bar which doesn't blend with the status bar, and then the view starts. All the 'solutions' at the other questions' answers' yield the same result for me.

I've also tried setting self.edgesForExtendedLayout = UIRectEdgeNone; or self.edgesForExtendedLayout = UIRectEdgeAll; but that also didn't have any impact.

How can I make my navigation bar transparent without messing up everything?

UPDATE: Following Warif Akhand Rishi's answer, I've changed self.navigationController.view.backgroundColor = [UIColor clearColor]; to self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];, now I'm getting a gray, unified status/navbar, but still not transparent:

enter image description here

UPDATE 2: I've hooked up the view debugger, and that gray background seems to come from deep down from the roots of view hierarchy, and my view's content is not extending up. I've tried self.edgesForExtendedLayout = UIRectEdgeAll; again with the latest code but still no avail:

enter image description here

like image 531
Can Poyrazoğlu Avatar asked Jan 10 '16 15:01

Can Poyrazoğlu


People also ask

How do I make my navigation bar translucent?

Creating a transparent navbar is very easy - just don't add a color class . bg-* to the navbar. In this case, the Navbar will take the color of the parent's background color.

What is translucent navigation bar?

Translucent or partially transparent is when the colour can be seen but the background behind the object is also slightly visible through it. For this we use. background-color: #FFFFFF; opacity: 0.5; filter: alpha(opacity=50);


5 Answers

swift 4 transparent nav bar: (be sure view extends behind nav bar to show through, otherwise will just be black)

navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage() //remove pesky 1 pixel line

or just match navbar color to color of your current vc, but keep it opaque. with translucent set to false child views will line up with navbar instead of going under it.

navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.barTintColor = UIColor.yourColor
navigationController?.navigationBar.shadowImage = UIImage() //remove pesky 1 pixel line
like image 81
MadeByDouglas Avatar answered Oct 18 '22 19:10

MadeByDouglas


Change your

self.navigationController.view.backgroundColor = [UIColor clearColor];

to this

self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
like image 36
Warif Akhand Rishi Avatar answered Oct 18 '22 18:10

Warif Akhand Rishi


Okay, after struggling, I've solved the problem on my own. There was more than one problem. It wasn't about the extended edges, it was about the line self.navigationController.view.backgroundColor = [UIColor clearColor]; (which had to be self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; as Warif Akhand Rishi suggested) and also my table view's clip subviews property. I've changed that line and also turned off clipping of my table view and now it works as expected.

like image 7
Can Poyrazoğlu Avatar answered Oct 18 '22 18:10

Can Poyrazoğlu


For iOS 13 and the UINavigationBarAppearance API:

let navAppearance = UINavigationBarAppearance()
navAppearance.configureWithTransparentBackground()
self.navigationItem.standardAppearance = navAppearance

Eliminate 5+ lines of shadow/background/color code!

like image 3
Leo Avatar answered Oct 18 '22 18:10

Leo


I'm a little late to the party, but I recently needed to do the same thing and I found the following actually works best (because it removes all shadows and bleed-throughs you may have from something lower in the stack):

guard let navBar = navigationController?.navigationBar else { return }
navBar.barStyle = .black
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
navBar.isTranslucent = true
navBar.isHidden = false
like image 2
BonanzaDriver Avatar answered Oct 18 '22 19:10

BonanzaDriver