Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Bar Appearance barTintColor alpha component

I'm trying to add some transparency to my NavigationBar without any success. Within my AppDelegate's didFinishLaunchingWithOptions, I have the following lines of code:

let navigationBarAppearance = UINavigationBar.appearance()
navigationBarAppearance.barTintColor = UIColor.blueColor().colorWithAlphaComponent(0.2)
navigationBarAppearance.translucent = true

But no mater what the alpha component is, the transparency does not change. I have even attempted to use a color UIImage:

let colorImage = UIImage.imageFromColor(UIColor.blueColor().colorWithAlphaComponent(0.2), frame: CGRect(x: 0, y: 0, width: 340, height: 64))
navigationBarAppearance.setBackgroundImage(colorImage, forBarMetrics: .Default)

That method alos doe not change the transparency of the navigation bar. Within a view controller I attempted:

self.navigationController?.navigationBar.tintColor = UIColor.blueColor()
self.navigationController?.navigationBar.alpha = 0.2
self.navigationController?.navigationBar.translucent = true

And still no luck. Can anyone help me figure out how I can add transparency to my navigation bar?

like image 899
Mike Walker Avatar asked Nov 29 '25 05:11

Mike Walker


1 Answers

Here you go man. Just follow these steps

First make a extension class. Add just a new empty swift file and name it as UIImageExtension or whatever you feel like

Write the following code in that extension

import Foundation
import UIKit
extension UIImage{
    static func imageFromColor(color: UIColor) -> UIImage {
    let rect = CGRectMake(0, 0, 1, 1)

    // create a 1 by 1 pixel context
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
    color.setFill()
    UIRectFill(rect)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image

    }
}

Now go to your ViewController class and write the following code and play with alpha. Thats it.

let image = UIImage.imageFromColor(UIColor(red: 0.071, green: 0.071, blue: 0.922, alpha: 0.2))
self.navigationController?.navigationBar.setBackgroundImage(image, forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.barStyle = .Default

For Swift 3.0

import Foundation
import Foundation
import UIKit
extension UIImage{
    static func imageFromColor(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)

        // create a 1 by 1 pixel context
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        color.setFill()
        UIRectFill(rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!

    }
}

USAGE

let image = UIImage.imageFromColor(color: UIColor(red: 0.071, green: 0.071, blue: 0.922, alpha: 0.2))
self.navigationController?.navigationBar.setBackgroundImage(image, for: UIBarMetrics.default)
self.navigationController?.navigationBar.barStyle = .default

I also made a sample. Have a look. Sample is in Swift 2.x version
https://github.com/RajanMaheshwari/TransparentNav
Hope it helps!

EDIT: Just changed the Navigation Bar to translucent and it worked enter image description here

Screenshot
enter image description here

like image 105
Rajan Maheshwari Avatar answered Dec 01 '25 19:12

Rajan Maheshwari