Skip to content

Instantly share code, notes, and snippets.

@scelis
Created December 21, 2009 18:11
Show Gist options
  • Save scelis/261120 to your computer and use it in GitHub Desktop.
Save scelis/261120 to your computer and use it in GitHub Desktop.
Add a background image to your UINavigationBar.
#import <UIKit/UIKit.h>
#import "SCClassUtils.h"
int main(int argc, char *argv[])
{
[SCClassUtils swizzleSelector:@selector(insertSubview:atIndex:)
ofClass:[UINavigationBar class]
withSelector:@selector(scInsertSubview:atIndex:)];
[SCClassUtils swizzleSelector:@selector(sendSubviewToBack:)
ofClass:[UINavigationBar class]
withSelector:@selector(scSendSubviewToBack:)];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
#import <UIKit/UIKit.h>
#define kSCNavigationBarBackgroundImageTag 6183746
#define kSCNavigationBarTintColor [UIColor colorWithRed:0.54 green:0.18 blue:0.03 alpha:1.0]
@interface SCAppUtils : NSObject
{
}
+ (void)customizeNavigationController:(UINavigationController *)navController;
@end
#import "SCAppUtils.h"
@implementation SCAppUtils
+ (void)customizeNavigationController:(UINavigationController *)navController
{
UINavigationBar *navBar = [navController navigationBar];
[navBar setTintColor:kSCNavigationBarTintColor];
UIImageView *imageView = (UIImageView *)[navBar viewWithTag:kSCNavigationBarBackgroundImageTag];
if (imageView == nil)
{
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigation-bar-bg.png"]];
[imageView setTag:kSCNavigationBarBackgroundImageTag];
[navBar insertSubview:imageView atIndex:0];
[imageView release];
}
}
@end
#import </usr/include/objc/objc-class.h>
#import <Foundation/Foundation.h>
@interface SCClassUtils : NSObject
{
}
+ (void)swizzleSelector:(SEL)orig ofClass:(Class)c withSelector:(SEL)new;
@end
#import "SCClassUtils.h"
@implementation SCClassUtils
+ (void)swizzleSelector:(SEL)orig ofClass:(Class)c withSelector:(SEL)new;
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if (class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
{
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
}
else
{
method_exchangeImplementations(origMethod, newMethod);
}
}
@end
#import <UIKit/UIKit.h>
@interface UINavigationBar (SCBackgroundImage)
- (void)scInsertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)scSendSubviewToBack:(UIView *)view;
@end
#import "UINavigationBar+SCBackgroundImage.h"
#import "SCAppUtils.h"
@implementation UINavigationBar (SCBackgroundImage)
- (void)scInsertSubview:(UIView *)view atIndex:(NSInteger)index
{
[self scInsertSubview:view atIndex:index];
UIView *backgroundImageView = [self viewWithTag:kSCNavigationBarBackgroundImageTag];
if (backgroundImageView != nil)
{
[self scSendSubviewToBack:backgroundImageView];
}
}
- (void)scSendSubviewToBack:(UIView *)view
{
[self scSendSubviewToBack:view];
UIView *backgroundImageView = [self viewWithTag:kSCNavigationBarBackgroundImageTag];
if (backgroundImageView != nil)
{
[self scSendSubviewToBack:backgroundImageView];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment