Created
December 21, 2009 18:11
-
-
Save scelis/261120 to your computer and use it in GitHub Desktop.
Add a background image to your UINavigationBar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment