Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent UINavigationBar

Am newbie to ios and I found this solution on making the UINavigationBar Transparent. Where in my project files can I put this code

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

So that it is applied in my entire project where navigation controller is being used.

like image 542
Nyaga Kennedy Avatar asked Feb 18 '14 10:02

Nyaga Kennedy


People also ask

How do I make my navigation bar clear?

You need to do three things to make a navigation bar transparent. Set background image to non-nil empty image ( UIImage() ). Set shadow image to non-nil empty image ( UIImage() ). Set isTranslucent to true .

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);


1 Answers

Put in your viewDidLoad function of your rootViewController this code:

Objective-C:

[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];

Swift 2.x:

if let navigationBar = navigationController?.navigationBar {
        navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
        navigationBar.shadowImage = UIImage()
        navigationBar.translucent = true
        navigationController?.view.backgroundColor = .clearColor()
    }

Swift 3:

if let navigationBar = navigationController?.navigationBar {
        navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationBar.shadowImage = UIImage()
        navigationBar.isTranslucent = true
        navigationController?.view?.backgroundColor = .clear
    }

This works for sure! Transparent UINavigationBar.

like image 88
Dimitris Bouzikas Avatar answered Oct 04 '22 06:10

Dimitris Bouzikas