Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ryanjm
Created November 2, 2011 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanjm/1332560 to your computer and use it in GitHub Desktop.
Save ryanjm/1332560 to your computer and use it in GitHub Desktop.
Issue with CGContext and iOS5
- (void)applicationDidFinishLaunching:(UIApplication *)application {
...
[self.navigationController.navigationBar setNeedsDisplay];
...
}
#import "OQCStyle.h"
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = [NSArray arrayWithObjects:(id)startColor, (id)endColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
void draw1PxStroke(CGContextRef context, CGPoint startPoint, CGPoint endPoint, CGColorRef color) {
CGContextSaveGState(context);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5);
CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
void drawGlossAndGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor) {
drawLinearGradient(context, rect, startColor, endColor);
CGColorRef glossColor1 = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.35].CGColor;
CGColorRef glossColor2 = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1].CGColor;
CGRect topHalf = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height/2);
drawLinearGradient(context, topHalf, glossColor1, glossColor2);
}
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
CGColorRef lightColor = [UIColor colorWithRed:255.0 green:255.0
blue:255.0 alpha:1.0].CGColor;
CGColorRef darkColor = [UIColor colorWithRed:196.0/255.0 green:196.0/255.0
blue:196.0/255.0 alpha:1.0].CGColor;
CGColorRef highlightColor = [UIColor colorWithWhite:0.9 alpha:1.0].CGColor;
CGColorRef borderColor = [UIColor colorWithRed:163.0/255.0 green:163.0/255.0
blue:163.0/255.0 alpha:1.0].CGColor;
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect navBarRect = self.bounds;
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, lightColor);
CGContextFillRect(context, navBarRect);
CGContextRestoreGState(context);
drawLinearGradient(context, navBarRect, lightColor, darkColor);
// Add top 1px border
CGPoint startPoint = CGPointMake(navBarRect.origin.x, navBarRect.origin.y);
CGPoint endPoint = CGPointMake(navBarRect.origin.x + navBarRect.size.width - 1, navBarRect.origin.y);
draw1PxStroke(context, startPoint, endPoint, highlightColor);
// Add bottom 1px border (hides separator)
CGPoint startPoint2 = CGPointMake(navBarRect.origin.x, navBarRect.origin.y + navBarRect.size.height - 1);
CGPoint endPoint2 = CGPointMake(navBarRect.origin.x + navBarRect.size.width - 1, navBarRect.origin.y + navBarRect.size.height - 1);
draw1PxStroke(context, startPoint2, endPoint2, borderColor);
if (SYSTEM_VERSION_LESS_THAN(@"5.0"))
self.tintColor = [OQCStyle orangeColor];
else {
self.tintColor = [UIColor colorWithRed:178.0/255.0 green:178.0/255.0
blue:178.0/255.0 alpha:1.0];
// self.tintColor = [UIColor redColor];
}
// [super drawRect:rect];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment