0

I added a UIToolBar to a UIViewController and added UIBarButton in it, and also added code for display image in background but image is not displayed

here is my code

UIBarButtonItem *b1=[[UIBarButtonItem alloc]initWithTitle:@"   Home   " style:UIBarButtonItemStyleBordered target:self action:@selector(home: )];
[b1 setImage:[UIImage imageNamed:@"b1image.png"]];

UIBarButtonItem *b3=[[    UIBarButtonItem alloc]initWithTitle:@"   WorkOuts   " style:UIBarButtonItemStyleBordered target:self action:nil];
[b1 setImage:[UIImage imageNamed:@"b3image.png"]];

UIBarButtonItem *b4=[[UIBarButtonItem alloc]initWithTitle:@"   All Exercises  " style:UIBarButtonItemStyleBordered target:self action:@selector(all:)];
[b1 setImage:[UIImage imageNamed:@"b4image.png"]];

NSArray *li=[[NSArray alloc]initWithObjects:b1,b3,b4, nil];

self.toolBar.items=li;
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Have a look at the documentation for UIBarButtonItem

You can use a UIButton to display the image:

UIImage *image = [UIImage imageNamed:@"b1image"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0.f, 0.f, image.size.width, image.size.height)];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(home:) forControlEvents:UIControlEventTouchUpInside];

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

This should fix your problem and set the image correctly. setImage: is inherited from another class and will have no affect on UIBarButtonItem.

btype
  • 1,593
  • 19
  • 32