2012-09-05 86 views
0

是否有任何方法來更改UIToolbar內的按鈕圖像?從代碼更改UIToolbar按鈕圖像

我實際上使用這種方式,但它不起作用。

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

對於您的信息,「myButton」是一個IBOulet UIBarButtonItem。

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

嘿,你只需將UIButton拖放到xib本身中即可。 – Spynet

回答

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

謝謝。

+0

呃,對於您的信息,這是一個UIBarButton,它是否像UIButton一樣工作? –

+0

是的,絕對是像烏布頓一樣工作。 –

1

//添加圖片吧在iphone

設置按鈕式的工具欄俗的按鈕項:

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