Iphone UINavigationBar渐变详细信息

Iphone UINavigationBar渐变详细信息,iphone,uinavigationbar,Iphone,Uinavigationbar,我正试图重现一个UINavigationBar的外观。条形图的背景是使用渐变绘制的,但不清楚其中的默认颜色和点是什么。有人在这方面做过什么吗?来自我的一个项目。根据你的喜好调整颜色。如果需要,它还可以显示背景图像(imageReady),否则它会像苹果一样绘制导航栏 // #Lighter r,g,b,a #Darker r,g,b,a #define MAIN_COLOR_COMPONENTS

我正试图重现一个UINavigationBar的外观。条形图的背景是使用渐变绘制的,但不清楚其中的默认颜色和点是什么。有人在这方面做过什么吗?

来自我的一个项目。根据你的喜好调整颜色。如果需要,它还可以显示背景图像(imageReady),否则它会像苹果一样绘制导航栏

//                                  #Lighter r,g,b,a            #Darker r,g,b,a
#define MAIN_COLOR_COMPONENTS       { 0.153, 0.306, 0.553, 1.0, 0.122, 0.247, 0.482, 1.0 }
#define LIGHT_COLOR_COMPONENTS      { 0.478, 0.573, 0.725, 1.0, 0.216, 0.357, 0.584, 1.0 }

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
    if (imageReady) {
        UIImage *img = [UIImage imageNamed: @"navigation_background.png"];
        [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    } else {
        // Render yourself instead.
        // You will need to adjust the MAIN_COLOR_COMPONENTS and LIGHT_COLOR_COMPONENTS to match your app

       // emulate the tint colored bar
       CGContextRef context = UIGraphicsGetCurrentContext();
       CGFloat locations[2] = { 0.0, 1.0 };
       CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();

       CGFloat topComponents[8] = LIGHT_COLOR_COMPONENTS;
       CGGradientRef topGradient = CGGradientCreateWithColorComponents(myColorspace, topComponents, locations, 2);
       CGContextDrawLinearGradient(context, topGradient, CGPointMake(0, 0), CGPointMake(0,self.frame.size.height/2), 0);
       CGGradientRelease(topGradient);

       CGFloat botComponents[8] = MAIN_COLOR_COMPONENTS;
       CGGradientRef botGradient = CGGradientCreateWithColorComponents(myColorspace, botComponents, locations, 2);
       CGContextDrawLinearGradient(context, botGradient,
       CGPointMake(0,self.frame.size.height/2), CGPointMake(0, self.frame.size.height), 0);
       CGGradientRelease(botGradient);

       CGColorSpaceRelease(myColorspace);


       // top Line
       CGContextSetRGBStrokeColor(context, 1, 1, 1, 1.0);
       CGContextMoveToPoint(context, 0, 0);
       CGContextAddLineToPoint(context, self.frame.size.width, 0);
       CGContextStrokePath(context);

       // bottom line
       CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
       CGContextMoveToPoint(context, 0, self.frame.size.height);
       CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
       CGContextStrokePath(context);
    }
}

@end

这比单一渐变(我以前使用的)要好得多,但是,即使在调整颜色后,它看起来仍然不正确。你的眼睛比我好…我对这些结果感到满意。什么不对劲?我会更深入地研究这个问题,看看为什么我觉得不对劲,但这比我认为答案要好得多。谢谢@嗨,我看到了你的答案,不知道你能否在这里给我一点指导。我应该在哪里添加上面的代码?此外,一旦添加,我如何实际调用和使用它?很抱歉提出了一些基本的问题,但你的建议将对我们很有帮助me@Zhen它被称为Objective-C类别,你可以在这里读到更多关于它的信息:它不是一个渐变,而是一个叠加的透明图像。有关详细信息,请参见此答案