public
Last active

Add a background image to your UINavigationBar.

  • Download Gist
SCAppUtils.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12
#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
SCAppUtils.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#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
SCClassUtils.h
Objective-C
1 2 3 4 5 6 7 8 9 10
#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
SCClassUtils.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#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
UINavigationBar+SCBackgroundImage.h
Objective-C
1 2 3 4 5 6 7 8
#import <UIKit/UIKit.h>
 
@interface UINavigationBar (SCBackgroundImage)
 
- (void)scInsertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)scSendSubviewToBack:(UIView *)view;
 
@end
UINavigationBar+SCBackgroundImage.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#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
main.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.