user1171478
user1171478

Reputation: 19

Add an image to a navigation bar

I want to change the image, the title and the color of a backButton in the navigationBar. Here the code:

UIButton* backButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BackButtonTopBar.png"]];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;

self.navigationController.navigationBar.tintColor = [UIColor blackColor]; 

UIImage *image = [UIImage imageNamed: @"TopBar.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;

but I have some problems:

  1. the black color not point
  2. the back image not working

So someone can help me? Thanks.

Upvotes: 0

Views: 3923

Answers (2)

Sree
Sree

Reputation: 907

UIButton * backButton = [UIButton buttonWithType:101];

[backButton setBackgroundImage:[UIImage imageNamed:BACK_BUTTON_IMAGE_NORMAL] forState:UIControlStateNormal];

    [backButton setBackgroundImage:[UIImage imageNamed:BACK_BUTTON_PRESSED] forState:UIControlStateHighlighted];
    UIView * backButtonView = [[UIView alloc] initWithFrame:CGRectMake(BACK_BUTTON_FRAME)];

    [backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];    

    [backButtonView addSubview:backButton];

    UIBarButtonItem * backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
    self.navigationItem.leftBarButtonItem = backBarButton;

[backButtonView release];

[backBarButton release];

Upvotes: 1

Lorenzo B
Lorenzo B

Reputation: 33428

What does this line of code mean?

UIButton* backButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BackButtonTopBar.png"]];

If you need to create a button, you have to do the following.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

and then set to it the background image.

But the way I prefer is to create a category extension for UIBarButtonItem like the following

//UIBarButtonItem+YourPreferredName.h    
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action;

//UIBarButtonItem+YourPreferredName.m    
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.titleLabel.textAlignment = UITextAlignmentCenter;

    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    // additional customizations here...

    UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return [barButtonItem autorelease];    
}

If you import UIBarButtonItem+YourPreferredName.h where you need it, then you can use as the following:

UIBarButtonItem* backBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"YoutImageName"] title:@"YourTitle" target:self action:@selector(goBack:)];

where goBack could be the following:

- (void)goBack:(id)sender
{
   // do stuff here
}

Hope it helps.

Upvotes: 0

Related Questions