如何使用渐变颜色设置UINavigationbar? [英] How can I set the UINavigationbar with gradient color?

查看:186
本文介绍了如何使用渐变颜色设置UINavigationbar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 UINavigationbar backgroundColor 设置为渐变颜色,我想通过数组设置它颜色创建一个渐变,理想情况下,作为 UINavigationBar 内的可访问方法,将其颜色更改为此渐变。

I want to set the UINavigationbar backgroundColor to a gradient color where I would like to set it via an array of colors to create a Gradient, ideally, as accessible methods inside UINavigationBar to change its color to this gradient.

有什么建议么? (除了手动设置图像作为导航栏的背景图像)

Any suggestions? (Aside from setting an image manually as the background image of the navigation bar)

推荐答案

创建渐变图层并将其添加为背景导航栏。

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];

用于从图层创建图片。

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

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

    UIGraphicsEndImageContext();

    return outputImage;
}

还有一件事,github中有一个库: CRGradientNavigationBar 你也可以使用这个库。

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

这篇关于如何使用渐变颜色设置UINavigationbar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆