First create an extension for CAGradientLayer (need to add Quartz library)
extension CAGradientLayer {
class func gradientLayerForBounds(bounds: CGRect) -> CAGradientLayer {
var layer = CAGradientLayer()
layer.frame = bounds
layer.colors = [UIColor.redColor().CGColor, UIColor.blueColor().CGColor]
return layer
}
}
Use whatever colors suit your needs.Then in your overriden NavController class (so many ways to do this, I chose to set it up in the NavController subclass used everywhere in my app),
private func imageLayerForGradientBackground() -> UIImage {
var updatedFrame = self.navigationBar.bounds
// take into account the status bar
updatedFrame.size.height += 20
var layer = CAGradientLayer.gradientLayerForBounds(updatedFrame)
UIGraphicsBeginImageContext(layer.bounds.size)
layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
In your viewDidLoad of the NavController subclass:override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.translucent = false
self.navigationBar.tintColor = UIColor.whiteColor()
let fontDictionary = [ NSForegroundColorAttributeName:UIColor.whiteColor() ]
self.navigationBar.titleTextAttributes = fontDictionary
self.navigationBar.setBackgroundImage(imageLayerForGradientBackground(), forBarMetrics: UIBarMetrics.Default)
}
I did some whitening to the tint and title text, mainly because any gradient you have, white would probably work best.Here's the current app I'm working on with the gradient colors of my choice: