Reputation: 2285
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
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
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