如何绘制与ION NavBar相同的渐变背景

时间:2011-08-22 08:17:32

标签: iphone objective-c

如何绘制与UINavBar相同的渐变背景?

我想在具有相同背景的UINavBar上添加一些视图。它们会掉阴影,所以我不能简单地将它们设置为透明。

==============================

我得到了解决方案,非常简单

+ (Class)layerClass
{
    return [CAGradientLayer class];
}

//
// setupGradientLayer
//
// Construct the gradient for either construction method
//
- (void)setupGradientLayer
{
    CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
    gradientLayer.colors =
        [NSArray arrayWithObjects:
            (id)[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor,
            (id)[UIColor colorWithRed:0.69 green:0.74 blue:0.8 alpha:1.0].CGColor,
            (id)[UIColor colorWithRed:0.43 green:0.51 blue:0.63 alpha:1.0].CGColor,
            (id)[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0].CGColor,
        nil];
    gradientLayer.locations = 
        [NSArray arrayWithObjects:
            [NSNumber numberWithFloat:0],
            [NSNumber numberWithFloat:0.02],
            [NSNumber numberWithFloat:0.98],
            [NSNumber numberWithFloat:1],
        nil];
    self.backgroundColor = [UIColor clearColor];
}

1 个答案:

答案 0 :(得分:0)

我认为您可以通过在自定义视图中创建CAGradientLayer来实现此目的。

你可以在这里找到关于此的文档: http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CAGradientLayer_class/Reference/Reference.html