// In your view controller:
- (void)loadView
{
// Create an UIButton
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
// Set its background image for some states...
[button setBackgroundImage: @"back-button.png" forState:UIControlStateNormal];
// Add target to receive Action
[button addTarget: self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
// Set frame width, height
button.frame = CGRectMake(0, 0, 50, 26);
// Add this UIButton as a custom view to the self.navigationItem.leftBarButtonItem
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView: button] autorelease];
}
- (void)loadView
{
// Create an UIButton
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
// Set its background image for some states...
[button setBackgroundImage: @"back-button.png" forState:UIControlStateNormal];
// Add target to receive Action
[button addTarget: self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
// Set frame width, height
button.frame = CGRectMake(0, 0, 50, 26);
// Add this UIButton as a custom view to the self.navigationItem.leftBarButtonItem
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView: button] autorelease];
}
// Your actual NavigationBar that you want to manipulate
#define MyNavBarTag 201889
// Set it to your image file (from the bundle)
#define NavBarBackgroundImage @"navbarbgrnd.png"
...
// UINavigationBar category
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
// if( self.tag == MyNavBarTag ){ // You can avoid unwanted side effects in your app
UIImage *image = [UIImage imageNamed: NavBarBackgroundImage];
[image drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
// }else{
// [super drawRect:rect];
// }
}
@end
#define MyNavBarTag 201889
// Set it to your image file (from the bundle)
#define NavBarBackgroundImage @"navbarbgrnd.png"
...
// UINavigationBar category
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
// if( self.tag == MyNavBarTag ){ // You can avoid unwanted side effects in your app
UIImage *image = [UIImage imageNamed: NavBarBackgroundImage];
[image drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
// }else{
// [super drawRect:rect];
// }
}
@end
self.navigationItem.hidesBackButton = YES;
// Add Left Button to act as Back Button
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"back-button.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(backTo)] autorelease];
self.navigationItem.leftBarButtonItem = backButton;