Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UINavigationBar's backButton?

I am making a iPhone app that allows the user to go back in a UINavigationBar. However, the way it looks now is horrendous. I am trying to customize it with 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 the 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 a way to remove that space? Is there a possibility I can use a UIButton so I can customize it completely? I did something in interface builder where i dragged a UIButton into the UINavigationBar's rightButton and it works perfectly. Is there anyway I can do it programmatically?

Thanks!

Here's how it looks:

UINavigationBar

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];

Here's 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
like image 763
iosfreak Avatar asked May 14 '11 16:05

iosfreak


People also ask

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.

How do I change the navigation bar on my Iphone?

A user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.


2 Answers

Combined the two answers and added the Back Button Functionality

// Custom Navigation Bar Back Button

// Create Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(0,0,54,32); // width=54, height=32

// Set Button Image
NSString *backButtonURL = [[NSBundle mainBundle]pathForResource:@"back" ofType:@"png"];
UIImage *backButtonImage = [UIImage imageWithContentsOfFile:backButtonURL];
[button setBackgroundImage:backButtonImage forState:UIControlStateNormal];

// Important: Set Button Action to go back
[button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
like image 172
James Gwee Avatar answered Sep 24 '22 14:09

James Gwee


If you want to add UIButton to navigation bar via obj-c, your code should look like this:

UIButton *button = /* initialize your button */

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
like image 29
akashivskyy Avatar answered Sep 23 '22 14:09

akashivskyy