4

I'm trying to recreate the look of a UINavigationBar. The background of the bar is drawn using a gradient, but it's unclear exactly what the default colors and points are in it. Has anyone done anything in this area?

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • It turns out it's not a gradient, but an overlayed transparent image. See this answer for details http://stackoverflow.com/questions/15304629/how-can-i-replicate-uinavigationbars-gradient-colors – Kenny Winker Aug 15 '13 at 20:37

1 Answers1

17

From one of my projects. Adjust the colors to your liking. It also can show a background image if you want (imageReady), else it draws the navbar like Apple's

//                                  #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
coneybeare
  • 33,113
  • 21
  • 131
  • 183
  • This is much better than a single gradient (what I was using before), but, even after adjusting colors, it still doesn't look right. – Ben Gottlieb Dec 06 '09 at 04:40
  • You have a better eye than me… I was pleased by these results. What seems off? – coneybeare Dec 06 '09 at 05:36
  • I'm going to look deeper into this to see why it doesn't look right to me, but it's so much better than I'm considering it answered. Thanks! – Ben Gottlieb Jan 02 '10 at 16:10
  • @coneybeare hi, I came across your answer and was wondering if you can guide me a little here. Where do I actually add the code above? Also once added, how do I actually call and use it? Sorry for the fundamental questions, but your advise will be very helpful to me – Zhen Jul 26 '11 at 06:06
  • @Zhen It is called an Objective-C Category, and you can read up more about it here: http://macdevelopertips.com/objective-c/objective-c-categories.html – coneybeare Jul 26 '11 at 13:16