Ghanshyam Tomar Ghanshyam Tomar - 1 year ago 185
iOS Question

how can i set the UINavigationbar with gradient color?

I want to set the

with gradient color where i would like to use the NSArray of colors for create Gradient color can anybody know how to code for that?
and also i used the methods for
for change its color.
it will be like i will give the color array for create gradient or something else easy can anybody suggest me is more better (except the setting the background gradient image)

Thanks in advance.

Answer Source

Create gradient layer and add it as background of navigation bar.

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.navigationController.navigationBar.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
    [self.navigationController.navigationBar setBackgroundImage:[self imageFromLayer:gradient] forBarMetrics:UIBarMetricsDefault];

For creating image from layer.

- (UIImage *)imageFromLayer:(CALayer *)layer
    UIGraphicsBeginImageContext([layer frame].size);

    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();


    return outputImage;

One more thing, there is one library available in github : CRGradientNavigationBar you can also use this library.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download