0

我在 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;
4

1 回答 1

0

查看文档_UIBarButtonItem

您可以使用 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.

于 2013-04-10T08:42:46.583 回答