Mangesh
Mangesh

Reputation: 2285

UIToolBar button image

I am using following code for add button with image on tool bar, but it is only showing color no image is displaying.

 UIBarButtonItem *locationItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"butImg.png"] style:UIBarButtonItemStylePlain target:self action:@selector(butImgPressed:)];  

Any help will be appreciated.

Upvotes: 1

Views: 2402

Answers (2)

Jignesh Chanchiya
Jignesh Chanchiya

Reputation: 803

Only way to do it:

UIButton *btnName = [UIButton buttonWithType:UIButtonTypeCustom];

[btnName setFrame:CGRectMake(0, 0, 23, 21)];

[btnName setBackgroundImage:[UIImage imageNamed:@"butImg.png"] forState:UIControlStateNormal]; 

[btnName addTarget:self action:@selector(butImgPressed:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *locationItem = [[UIBarButtonItem alloc] initWithCustomView:btnName];

Upvotes: 3

Guntis Treulands
Guntis Treulands

Reputation: 4762

You want to know why it doesn't work?

The reason UIToolBar was creating the white mask, was because the UIToolBar doesn't allow color images on it by default.

So the only way to accomplish what You want is by using UIBarButtonItem with UIButton as customView.

Upvotes: 4

Related Questions