I am making an iPhone application that allows the user to return to the UINavigationBar. However, what he looks like now is terrible. I am trying to customize it using my own image (minus the default UIBarButtonItem background). My image includes my custom background, but you can still see part of the button on the left and right.
UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_button_normal.png"] style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)] autorelease]; self.navigationItem.leftBarButtonItem = cancelButton;
Is there any way to remove this space? Is it possible to use UIButton to fully configure it? I did something in the interface builder where I dragged the UIButton to the right byte of the UINavigationBar and it works fine. Anyway, can I do this programmatically?
Thanks!
Here's what it looks like:

EDIT # 1:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchDown]; [button setTitle:@"Show View" forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageNamed:@"back_button_normal.png"] forState:UIControlStateNormal]; UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; [self.navigationItem setLeftBarButtonItem:barButtonItem];
This is how I make my background for the UINavigationBar (places in my RootViewController):
@implementation UINavigationBar (UINavigationBarCustomDraw) - (void) drawRect:(CGRect)rect { [self setTintColor:[UIColor colorWithRed:0.85f green: 0.85f blue:0.85f alpha:1]]; if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:@"Back to ..."]) { [[UIImage imageNamed:@"UINavigationBar_background.png"] drawInRect:rect]; CGRect frame = CGRectMake(0, 0, 320, 44); UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease]; [label setBackgroundColor:[UIColor clearColor]]; label.font = [UIFont boldSystemFontOfSize: 20.0]; label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1]; label.textAlignment = UITextAlignmentCenter; label.textColor = [UIColor whiteColor]; label.text = self.topItem.title; self.topItem.titleView = label; } else { [[UIImage imageNamed:@"login_button.png"] drawInRect:rect]; self.topItem.titleView = [[[UIView alloc] init] autorelease]; } } @end
source share