skor33393
skor33393

Reputation: 31

Can't set image to UINavigationBar's rightTabBarItem

I'm trying to set image to rightBarButtonItem.

I tried to do this 2 ways:

/* first way */
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: @"image.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(rightBarButtonItemTapped) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];

/* second way */
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image"]
                                                                       style:UIBarButtonItemStyleBordered
                                                                      target:self
                                                                      action:@selector(rightBarButtonItemTapped)];

P.S. When I tap right side of navigation bar I see that rightBarButtonItemTapped method invoked.

Upvotes: 0

Views: 46

Answers (1)

Chris1994
Chris1994

Reputation: 93

You can set an image to a bar button using the attribute inspector.

enter image description here

Upvotes: 1

Related Questions