151

I'm creating an app and i've browsed on the internet and i'm wondering how they make this transparent UINavigationBar like this:

enter image description here

I've added following like in my appdelegate:

UINavigationBar.appearance().translucent = true

but this just makes it look like following:

enter image description here

How can I make the navigation bar transparent like first image?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
  • With the code I don't know, but if you're good with CSS, you can use a framework (Pixate : http://www.freestyle.org/), and could just apply a CSS style to your navbar :) ! – Nicolas Charvoz Sep 15 '14 at 10:26

23 Answers23

322

You can apply Navigation Bar Image like below for Translucent.

Objective-C:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                     forBarMetrics:UIBarMetricsDefault]; //UIImageNamed:@"transparent.png"
self.navigationController.navigationBar.shadowImage = [UIImage new];////UIImageNamed:@"transparent.png"
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];

Swift 3:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) //UIImage.init(named: "transparent.png")
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
    
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
  • 4
    when i setBackgroundImage it removes the barTintColor? – Peter Pik Sep 15 '14 at 11:38
  • You can get image of Navigation bar..As per your need..and apply to navigation bar as above..(Image you want to apply to navigation bar) – Vidhyanand Sep 15 '14 at 15:08
  • does this code really works?I want the same kind of navigation bar in my app.How did you do that? – abhimuralidharan May 22 '15 at 06:33
  • 2
    setting the backgroundColor of the navigationController isn't necessary – matt bezark Dec 30 '15 at 00:04
  • 5
    With this solution I get a black navigationBar, any ideas ? – Carlos del Blanco Feb 08 '16 at 11:54
  • @CarlosdelBlanco do you have "self.edgesForExtendedLayout = UIRectEdgeNone;"? If so you need to remove it – Mário Carvalho Feb 08 '16 at 13:46
  • U need to set image like transparent.png as above. Transparent.png is the image u want to display as transparent..! – Vidhyanand Feb 08 '16 at 15:09
  • Yes, without the code "self.edgesForExtendedLayout = UIRectEdgeNone;" it works !! But now, I present the view with a UINavigationController and the navigationbar in the next view is a little translucent but not all and I put alpha = 0.2f. Any ideas ? – Carlos del Blanco Feb 11 '16 at 16:32
  • Is there any toggle to this in storyboard? – Happiehappie Nov 29 '16 at 02:03
  • I used this and it's working well But I want when user scroll to the down the color of navigation Bar increase - I used alpha But alpha will change the color to the black how can I do that ? – Saeed Rahmatolahi Jan 24 '18 at 10:52
  • Thanks, I made my navigation bar transperant, but as u said, my navaigation bar's color is set to white. So, to make it default, i set barTintColor of navigation bar and it works fine. Thank you @PeterPik – Arpit B Parekh Oct 25 '18 at 09:39
  • 1
    Not as expected. When you do it, you won't have the wall translucid like with an alpha of 0.7 right, PLUS covering the status bar as well. If you do this on iOS 13, you will have just a rectangle of nice good looking and above of it, THE STATUS BAR CRYSTAL CLEAR. which the author of the question its clear and specific. – Marlhex Oct 23 '19 at 22:34
  • in my case I wanted to reduce the transparency . – vishal dharankar Dec 17 '19 at 05:35
  • 1
    How to make it again as default color? – Yogendra Patel Mar 28 '20 at 13:11
  • 1
    It seems like this "workaround" for getting the navbar transparent stopped working in xCode Version 11.4. It used to work... Is it only me or has anyone else also experienced this? – Jens Apr 06 '20 at 09:32
  • The Chris Chute Swift 5 answer worked for me in xCode 12. An obvious statement, so please forgive. Ensure that your view goes to the "unsafe" top so there is something to go under the nav and status bars. – Ben Jan 18 '21 at 16:00
  • self.navigationController?.view.backgroundColor = .clear did not work for me in Swift 5. What did work was self.navigationController?.navigationBar.backgroundColor = .clear (swap navigationBar for view). – Ben Aug 29 '21 at 18:18
  • 1
    @YogendraPatel we can make it default color again by assigning ``` navigationController?.navigationBar.setBackgroundImage(nil, for: .default) navigationController?.navigationBar.shadowImage = nil navigationController?.navigationBar.isTranslucent = false navigationController?.view.backgroundColor = .clear ``` – Aviraj Patel Oct 11 '22 at 17:05
135

Swift Solution

This is the best way that I've found. You can just paste it into your appDelegate's didFinishLaunchingWithOptions method:

Swift 3 / 4

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    // Sets background to a blank/empty image
    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    // Sets shadow (line below the bar) to a blank image
    UINavigationBar.appearance().shadowImage = UIImage()
    // Sets the translucent background color
    UINavigationBar.appearance().backgroundColor = .clear
    // Set translucent. (Default value is already true, so this can be removed if desired.)
    UINavigationBar.appearance().isTranslucent = true
    return true
}

Swift 2.0

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    // Sets background to a blank/empty image
    UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
    // Sets shadow (line below the bar) to a blank image
    UINavigationBar.appearance().shadowImage = UIImage()
    // Sets the translucent background color
    UINavigationBar.appearance().backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
    // Set translucent. (Default value is already true, so this can be removed if desired.)
    UINavigationBar.appearance().translucent = true

    return true
}

source: Make navigation bar transparent regarding below image in iOS 8.1

kelin
  • 11,323
  • 6
  • 67
  • 104
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
  • 4
    Your Swift 3 solution just makes my bar plain white. – Hedylove Apr 06 '17 at 04:47
  • @JozemiteApps try making a brand new Xcode project and paste the code in. Should only take 3 minutes to confirm if it's my code above or something about your project that's causing this. – Dan Beaulieu Apr 06 '17 at 05:18
  • 2
    I also got a plain white navibar and no transparent navibar – Kingalione Oct 12 '17 at 09:11
  • This works great, thank you! Do you know how you could implement it such that the only the navigation bars on desired ViewControllers are transparent? – RufusV Sep 18 '18 at 06:06
  • 1
    @JoseRamirez It's probably the background of the viewController that you see. You need to change the top constraint of the first view to align with the superview and not the safe area or margin. – turingtested Feb 13 '20 at 09:32
  • Thanks, this is excellent. It also helps with SwiftUI if we want to hide the NavigationBar- we need to set the title to `""` and hidden to `true`, but adding your solution removes cases where the bar tint/background still shows when swiping back. Great! – user1354934 Nov 07 '20 at 23:21
67

Swift 5 applying only to the current view controller

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Make the navigation bar background clear
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.isTranslucent = true
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Restore the navigation bar to default
    navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
    navigationController?.navigationBar.shadowImage = nil
}
Chris Chute
  • 3,229
  • 27
  • 18
16

Swift 3 : extension for Transparent Navigation Bar

extension UINavigationBar {
    func transparentNavigationBar() {
    self.setBackgroundImage(UIImage(), for: .default)
    self.shadowImage = UIImage()
    self.isTranslucent = true
    }
}
Ankit Kumar Gupta
  • 3,994
  • 4
  • 31
  • 54
14

Swift 4.2 Solution: For transparent Background:

  1. For General Approach:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = true
    
    }
    
  2. For Specific Object:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        navBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        navBar.shadowImage = UIImage()
        navBar.navigationBar.isTranslucent = true
    
    }
    

Hope it's useful.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
14

I had been working on this, and I was facing a problem using the responses provided here by different users. Problem was a white box behind my NavigationBar transparent image on iOS 13+

enter image description here

My solution is this one

if #available(iOS 13, *) {
    navBar?.standardAppearance.backgroundColor = UIColor.clear
    navBar?.standardAppearance.backgroundEffect = nil
    navBar?.standardAppearance.shadowImage = UIImage()
    navBar?.standardAppearance.shadowColor = .clear
    navBar?.standardAppearance.backgroundImage = UIImage()
}

Update

thanks to @TMin

If you use a tableView/CollectionView with this you will notice a 1 point shadow appears when you scroll. Add navBar?.scrollEdgeAppearance = nil to get ride of this shadow.

Hope this helps anyone with same problem

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
10

I was able to accomplish this in swift this way:

let navBarAppearance = UINavigationBar.appearance()
let colorImage = UIImage.imageFromColor(UIColor.morselPink(), frame: CGRectMake(0, 0, 340, 64))
navBarAppearance.setBackgroundImage(colorImage, forBarMetrics: .Default)

where i created the following utility method in a UIColor category:

imageFromColor(color: UIColor, frame: CGRect) -> UIImage {
  UIGraphicsBeginImageContextWithOptions(frame.size, false, 0)
  color.setFill()
  UIRectFill(frame)
  let image = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  return image
}
Julian B.
  • 3,605
  • 2
  • 29
  • 38
8

What it worked for me:

    let bar:UINavigationBar! =  self.navigationController?.navigationBar
    self.title = "Whatever..."
    bar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    bar.shadowImage = UIImage()
    bar.alpha = 0.0 
7

Set the background property of your navigationBar, e.g.

navigationController?.navigationBar.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.5)

(You may have to change that a bit if you don't have a navigation controller, but that should give you an idea of what to do.)

Also make sure that the view below actually extends under the bar.

Atomix
  • 13,427
  • 9
  • 38
  • 46
  • This gives me following: http://i.stack.imgur.com/GT3WV.png How can i make a bit more red like the first link(image) i posted – Peter Pik Sep 15 '14 at 10:45
  • You can play with the alpha value and change it from 0.5 to anything between 0.0 and 1.0. And as I said, make sure the view below goes under the navigation bar, otherwise you won't see any content shine through the bar. If you are using Interface Builder, you can drag and align the top edge of that view with the upper edge of the screen. – Atomix Sep 15 '14 at 10:54
4

If you want to be able to do this programmatically in swift 4 while staying on the same view,

if change {
        navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.backgroundColor = UIColor(displayP3Red: 255/255, green: 206/255, blue: 24/255, alpha: 1)
        navigationController?.navigationBar.barTintColor = UIColor(displayP3Red: 255/255, green: 206/255, blue: 24/255, alpha: 1)
    } else {
        navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.setBackgroundImage(backgroundImage, for: .default)
        navigationController?.navigationBar.backgroundColor = .clear
        navigationController?.navigationBar.barTintColor = .clear
    }

One important thing to remember though is to click this button in your storyboard. I had an issue with a jumping display for a long time. Make sureyou set this: enter image description here

Then when you change the translucency of the navigation bar it will not cause the views to jump as the views extend all the way to the top, regardless of the visiblity of the navigation bar.

paul_f
  • 1,296
  • 17
  • 20
4

Add this in your did load

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.0)
//adjust alpha according to your need 0 is transparent 1 is solid
Abhishek Maurya
  • 697
  • 7
  • 19
3

For those looking for OBJC solution, to be added in App Delegate didFinishLaunchingWithOptions method:

[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[UINavigationBar appearance].shadowImage = [UIImage new];
[UINavigationBar appearance].backgroundColor = [UIColor clearColor];
[UINavigationBar appearance].translucent = YES;
david72
  • 7,151
  • 3
  • 37
  • 59
2

Try this, it works for me if you also need to support ios7, it is based on the transparency of UItoolBar:

[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];
    UIToolbar* blurredView = [[UIToolbar alloc] initWithFrame:self.navigationController.navigationBar.bounds];
    [blurredView setBarStyle:UIBarStyleBlack];
    [blurredView setBarTintColor:[UIColor redColor]];
    [self.navigationController.navigationBar insertSubview:blurredView atIndex:0];
EmilDo
  • 1,177
  • 3
  • 16
  • 33
2

iOS 13.0+ introduced UINavigationBarAppearance because of which, this problem occurs on iOS 13.0+

Use this to solve.

Change Navigation Bar Appearance Use UINavigationBarAppearance and UIBarButtonItemAppearance to change the appearance of the navigation bar.

// Make the navigation bar's title with red text.

if #available(iOS 13, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor.systemRed
        appearance.titleTextAttributes = [.foregroundColor: UIColor.lightText] // With a red background, make the title more readable.
        navigationItem.standardAppearance = appearance
        navigationItem.scrollEdgeAppearance = appearance
        navigationItem.compactAppearance = appearance // For iPhone small navigation bar in landscape.
    }
Avneesh Agrawal
  • 916
  • 6
  • 11
1

Utility method which you call by passing navigationController and color which you like to set on navigation bar. For transparent you can use clearColor of UIColor class.

For objective c -

+ (void)setNavigationBarColor:(UINavigationController *)navigationController 
                               color:(UIColor*) color {
   [navigationController setNavigationBarHidden:false animated:false];
   [navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
   [navigationController.navigationBar setShadowImage:[UIImage new]];
   [navigationController.navigationBar setTranslucent:true];
   [navigationController.view setBackgroundColor:color];
   [navigationController.navigationBar setBackgroundColor:color];
}

For Swift 3.0 -

class func setNavigationBarColor(navigationController : UINavigationController?, 
                                 color : UIColor) {
    navigationController?.setNavigationBarHidden(false, animated: false)
    navigationController?.navigationBar .setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.translucent = true
    navigationController?.view.backgroundColor = color
    navigationController?.navigationBar.backgroundColor =  color
}
Rahul
  • 10,457
  • 4
  • 35
  • 55
1

Write these two lines:

 navigationController?.navigationBar.isTranslucent = true
 navigationController?.navigationBar.backgroundColor = .clear

Worked for me in iOS 13

indrajit
  • 303
  • 1
  • 14
0

None of the answers here fully worked for me. This makes the navigation bar fully transparent - tested on iOS 14 and iOS 11 (Objective C):

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
Si.
  • 379
  • 2
  • 7
0

Anyone looking for a iOS 15+ working version, this is what worked for me, as the old techniques with setBackgroundImage/shadowImage were not working anymore.

To se it transparent:

func setTransparent() {
    backgroundColor = .clear
    isTranslucent = true

    standardAppearance.shadowColor = .clear
    standardAppearance.backgroundColor = .clear
    standardAppearance.backgroundEffect = nil
    scrollEdgeAppearance = standardAppearance
}

To remove transparency:

func removeTransparent() {
    setBackgroundImage(nil, for: .default)
    shadowImage = nil
    backgroundColor = .white
    isTranslucent = false

    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    standardAppearance = appearance
    scrollEdgeAppearance = standardAppearance
}
Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
0

My implementation of navigation bar configuration as translucent and switching to default state for iOS 15 and older versions:

extension UINavigationBar {
    static let defaultBackgroundColor = UIColor.red
    static let defaultTintColor = UIColor.white
    
    func setTranslucent(tintColor: UIColor, titleColor: UIColor) {
        if #available(iOS 15, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.titleTextAttributes = [.foregroundColor: titleColor]
            standardAppearance = appearance
            scrollEdgeAppearance = appearance
        } else {
            titleTextAttributes = [.foregroundColor: titleColor]
            setBackgroundImage(UIImage(), for: UIBarMetrics.default)
            shadowImage = UIImage()
        }
        isTranslucent = true
        self.tintColor = tintColor
    }
    
    func setDefaultState() {
        isTranslucent = false
        clipsToBounds = false
        
        if #available(iOS 15, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = UINavigationBar.defaultBackgroundColor
            appearance.titleTextAttributes = [.foregroundColor: UINavigationBar.defaultTintColor]
            
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
        } else {
            setBackgroundImage(UIImage(), for: UIBarPosition.any, barMetrics: UIBarMetrics.defaultPrompt)
            shadowImage = UIImage()
            barTintColor = UINavigationBar.defaultBackgroundColor
            titleTextAttributes = [.foregroundColor: UINavigationBar.defaultTintColor]
        }
        
        tintColor = UINavigationBar.defaultTintColor
    }
}
0

This will defiantly work for swift 4/5 users.

func setUpNavBar(){
    navigationItem.title = "Flick"
    navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.view.backgroundColor = UIColor.clear
    navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}
Navdeep Paliwal
  • 349
  • 3
  • 7
0

IOS15 Version

extension UIViewController {

    func clearNavigationBar(clear: Bool) {
        if clear {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            self.navigationController?.navigationBar.standardAppearance = appearance
            self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
        } else {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            self.navigationController?.navigationBar.standardAppearance = appearance
            self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
        }
    }
}


class ViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
         clearNavigationBar(clear: true)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
         super.viewWillDisappear(animated)
         clearNavigationBar(clear: false)
    }
}
0

For above all iOS version

if #available(iOS 15.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundImage = UIColor.clear.imageWithColor(width: UIScreen.main.bounds.size.width, height: 84)
        appearance.shadowImage = UIImage()
        appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.black ,NSAttributedString.Key.font : UIFont(name: "SF UI Display Semibold", size: 18) ?? UIFont()]
        appearance.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 2)
        self.navigationBar.standardAppearance = appearance
    } else {
        self.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationBar.shadowImage = UIImage()
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.black ,NSAttributedString.Key.font : UIFont(name: "SF UI Display Semibold", size: 18) ?? UIFont()]
        self.navigationBar.setTitleVerticalPositionAdjustment(2, for: UIBarMetrics.default)
    }


func imageWithColor(width: CGFloat, height: CGFloat) -> UIImage {
    let size = CGSize(width: width, height: height)
    return UIGraphicsImageRenderer(size: size).image { rendererContext in
        self.setFill()
        rendererContext.fill(CGRect(origin: .zero, size: size))
    }
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 31 '22 at 10:59
0

Just add bellow code line inside your application delegate

       UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)

       // Sets shadow (line below the bar) to a blank image
       UINavigationBar.appearance().shadowImage = UIImage()

       // Sets the translucent background color
       UINavigationBar.appearance().backgroundColor = .clear
       // Set translucent. (Default value is already true, so this can be removed if desired.)

       UINavigationBar.appearance().isTranslucent = true

then override your custom nav bar inside your view controller and make sure to reset once it disappear