从代码更改UIToolbar按钮图像

时间:2012-09-05 07:14:09

标签: objective-c xcode

有没有办法更改UIToolbar中的按钮图像?

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

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

为了您的信息,“myButton”是一个IBOulet UIBarButtonItem。

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

2 个答案:

答案 0 :(得分: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];

谢谢,

答案 1 :(得分: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];