Skip to content

Instantly share code, notes, and snippets.

@darkseed
Created December 5, 2011 11:24
Show Gist options
  • Save darkseed/1433288 to your computer and use it in GitHub Desktop.
Save darkseed/1433288 to your computer and use it in GitHub Desktop.
Custom UINavigation background
//
// CCustomNavigationBar.h
//
// Created by Tom Mulder on 05-12-11.
// Copyright (c) 2011 Reed Business. All rights reserved.
//
#define kCCNavBarImageTag 6183747
@interface CCustomNavigationBar : UINavigationController
@end
//
// CCustomNavigationBar.m
//
// Created by Tom Mulder on 05-12-11.
// Copyright (c) 2011 Reed Business. All rights reserved.
//
#import "CCustomNavigationBar.h"
@implementation CCustomNavigationBar
- (id)init
{
if ((self = [super init]))
{
[self.navigationBar setTintColor:[UIColor colorWithRed:231.0/255.0 green:87.0/255.0 blue:65.0/255.0 alpha:1.0]];
if ([self.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavigationBar"] forBarMetrics:UIBarMetricsDefault];
}
else
{
UIImageView *imageView = (UIImageView *)[self.navigationBar viewWithTag:kCCNavBarImageTag];
if (imageView == nil)
{
imageView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"NavigationBar"]];
[imageView setTag:kCCNavBarImageTag];
[imageView setFrame:CGRectMake(0, 0,
self.navigationBar.frame.size.width, self.navigationBar.frame.size.height)];
[self.navigationBar insertSubview:imageView atIndex:0];
[imageView release];
}
}
}
return self;
}
- (UIModalPresentationStyle)modalPresentationStyle
{
if ([self topViewController])
{
return [[self topViewController] modalPresentationStyle];
}
else
{
return [super modalPresentationStyle];
}
}
- (void)updateNavBarBackground
{
[[self.navigationBar viewWithTag:kCCNavBarImageTag] setFrame:CGRectMake(0, 0,
self.navigationBar.frame.size.width, self.navigationBar.frame.size.height)];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self updateNavBarBackground];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment