UIBarButton背景图像显示问题

时间:2013-04-10 08:36:39

标签: ios iphone image background uitoolbar

我在UIViewController中添加了一个UIToolBar并在其中添加了UIBarButton,并且还在后台添加了显示图像的代码但是没有显示图像

这是我的代码

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;

1 个答案:

答案 0 :(得分:0)

查看UIBarButtonItem

documentation

您可以使用UIButton显示图像:

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

这应该可以解决您的问题并正确设置图像。 setImage:继承自另一个类,对UIBarButtonItem没有任何影响。