Swift UINavigationBar背景颜色与主要颜色不同

maz*_*ati 13 uinavigationbar ios swift

问题是我为UINavigationBar设置的颜色与我为图层设置的颜色不同.我希望它是一样的!:)(见图)

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()]
Run Code Online (Sandbox Code Playgroud)

viewDidLoad()中的MyController.swift

view.backgroundColor = primaryColor
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我认为问题出在UIImage扩展中.请帮我解决这个问题.

解决了

UINavigationBar.appearance().translucent = false
Run Code Online (Sandbox Code Playgroud)

Vin*_*ino 11

只是uncheckTranslucent属性Navigation Bar。它对我有用。