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

时间:2016-02-09 19:39:18

标签: ios swift uinavigationbar

问题是我为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()]

viewDidLoad()中的MyController.swift

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

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

解决

UINavigationBar.appearance().translucent = false

1 个答案:

答案 0 :(得分:3)

uncheck的{​​{1}}属性Translucent。对我有用。