0

有什么方法可以更改 UIToolbar 中的按钮图像?

我实际上正在使用这种方式,但它不起作用。

[myButton setImage:[UIImage imageNamed:@"blue.png"]];

供您参考,“myButton”是一个 IBOUlet UIBarButtonItem。

@property (nonatomic, strong) IBOutlet UIBarButtonItem *myButton;
4

2 回答 2

2

使用以下油菜。我希望它对你有用。

UIImage *image = [UIImage imageNamed:@"buttonImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );    
[button setImage:image forState:UIControlStateNormal];
[button addTarget:myTarget action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];    
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

谢谢,。

于 2012-09-05T07:32:16.707 回答
1

// 将图像添加到 iphone 工具栏的栏按钮项

将按钮类型设置为 custum:

button=[UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 40, 40)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

// 在工具栏上添加图像:

 [toolbar insertSubview:[[[UIImageView alloc]
                         initWithImage:[UIImage imageNamed:@"image.png"]] autorelease] atIndex:1]; 
于 2012-09-05T08:29:52.900 回答