Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift UINavigationBar background color is different from main one

The problem is that color that I set for the UINavigationBar is different from the same color, which I have set for the layer. I want it to be the same! :) (see image)

AppDelegate.swift

UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().setBackgroundImage(UIImage.imageWithColor(primaryColor), forBarMetrics: .Default)
    UINavigationBar.appearance().barTintColor = primaryColor
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(17), NSForegroundColorAttributeName:UIColor.whiteColor()]

MyController.swift in viewDidLoad()

view.backgroundColor = primaryColor

Extensions.swift

let primaryColor = UIColor(red: 132/255, green: 205/255, blue: 93/255, alpha: 1)
extension UIImage {
    class func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }
}

enter image description here

I think the problem is in UIImage extension. Please help me to solve this problem.

Solved

UINavigationBar.appearance().translucent = false
like image 511
mazorati Avatar asked Feb 09 '16 19:02

mazorati


1 Answers

Just uncheck the Translucent property of Navigation Bar. It's worked for me.

like image 145
Vinoth Vino Avatar answered Oct 10 '22 03:10

Vinoth Vino