Haagenti
Haagenti

Reputation: 8144

iOS custom right navigation bar

I'm trying to set an image for a right bar item in my navigation controller but iOS 6 keeps showing a black glow. I have tried a number of solutions from stack overflow but can't get it to work. The current code I have is this:

UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gear"]
                                                              style:UIBarButtonItemStylePlain
                                                             target:self
                                                             action:@selector(someMethod)];
[rightItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];

[[self navigationItem] setRightBarButtonItem:rightItem];

In iOS7 is looks like this which is what I want: What I want This is how it looks in iOS6 What I currently have

Upvotes: 4

Views: 4496

Answers (4)

VarshaSai
VarshaSai

Reputation: 31

Try this:

UIImage* image = [UIImage imageNamed:@"sample_Image.png"];

CGRect frameimg = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(MethodName:)
     forControlEvents:UIControlEventTouchUpInside];   

UIBarButtonItem *barBtn =[[UIBarButtonItem alloc] initWithCustomView:someButton];

    [self.navigationItem setRightBarButtonItem:barBtn];

Upvotes: 0

Orifjon
Orifjon

Reputation: 1097

Use custom view:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; // change this to use your image
[rightButton addTarget:self
                    action:@selector(yourAction:)
          forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightButtonItem;

Upvotes: 0

Dhaval Bhadania
Dhaval Bhadania

Reputation: 3089

try this one :

UIImage *faceImage = [UIImage imageNamed:@"gear.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );//set bound as per you want
[face addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.rightBarButtonItem = backButton;

may it will help you.

Upvotes: 10

Bob de Graaf
Bob de Graaf

Reputation: 2722

Create a UIButton first with the image, then do:

[[UIBarbuttonItem alloc] initWithCustomView:button];

Upvotes: 0

Related Questions