Ghanshyam Tomar Ghanshyam Tomar - 26 days ago 15
iOS Question

how can i set the UINavigationbar with gradient color?

I want to set the

UINavigationbar
Backgroundcolor
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
UINavigationbar
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

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();

    UIGraphicsEndImageContext();

    return outputImage;
}

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

Comments