Skip to content

Instantly share code, notes, and snippets.

@danielphillips
Created February 8, 2012 22:11
Show Gist options
  • Save danielphillips/1774444 to your computer and use it in GitHub Desktop.
Save danielphillips/1774444 to your computer and use it in GitHub Desktop.
Create a solid colour (no Apple gloss) UINavigationBar
@interface DJPAppDelegate : UIResponder <UIApplicationDelegate, UIAppearanceContainer>
@end
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
}
@end
@implementation DJPAppDelegate
// missing code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
DJPMasterViewController *masterViewController = [[[DJPMasterViewController alloc] initWithNibName:@"DJPMasterViewController" bundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
if([UINavigationBar respondsToSelector:@selector(appearance)]){
UIImage *image = [UIImage imageNamed:@"bg.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
return YES;
}
// missing code
@end
@danielphillips
Copy link
Author

iOS 5
This brings the appearance proxy which allows you to define a background image onto the UINavigationBar's appearance. Providing a solid coloured image the same dimensions as your bar will make the application use this to override the default Apple style.
In order to use this you must implement the UIApplicationDelegate protocol.
This would crash in earlier iOS systems so in order to be safe you must wrap this code inside a respondsToSelector: check ensuring this doesn't attempt to be executed on devices running earlier versions of iOS operating system.

iOS 4.3 and earlier
You can use a Category to override the drawRect method for all UINavigationBar objects, specifying a colour and filling in the frame with that colour gives you the desired solid bar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment