Reputation: 309
I am having a terrible time trying to use images instead of titles for my UIBarButtonItems. I was under the understanding that setting the image for a UIBarButtonItem automatically scales the image to correct size for the UIBarButtonItem. This would make sense, as there doesn't seem to be any way to resize the image (you can't set an imageView, or add a subview, or anything). However when I use two different images of the exact same dimensions (24x24), I get different results:
myAddButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped)];
self.navigationItem.rightBarButtonItem = myAddButton;
myLeftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"gear.png"] style:UIBarButtonSystemItemAdd target:self action:@selector(settingsButtonTapped)];
self.navigationItem.leftBarButtonItem = myLeftButton;
Leads to the following:
Again, gear.png and plus.png are the same size, 24x24. I have confirmed this in Photoshop.
Now if I take the EXACT SAME lines of code, just replace gear with plus:
myAddButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped)];
self.navigationItem.rightBarButtonItem = myAddButton;
myLeftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonSystemItemAdd target:self action:@selector(settingsButtonTapped)];
self.navigationItem.leftBarButtonItem = myLeftButton;
I get this:
The difference in color being due to one being enabled and the other not.
I would just make them UIButtons, so that I could use imageViews and manually set this myself, except that I like the background that using UIBarButtonItem gives me, and I don't want to recreate it in Photoshop.
Any ideas as to what's happening here, and how I can fix it?
Thanks in advance.
EDIT:
Turns out that this issue was caused because I needed to clear my build and delete the app off of the simulator, as it was pulling from a 48x48 image that I had used earlier.
Upvotes: 0
Views: 979
Reputation: 42977
self.navigationItem.leftBarButtonItem =[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped)]autorelease];
Upvotes: 0
Reputation: 11770
Make sure you have the right image in your project. And, yeah, try cleaning your project sometimes. Removing the app from device/simulator can also be helpful.
Upvotes: 2
Reputation: 4249
Here's what you can try
UIButton *leftButton = [ UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setFrame:CGRectMake(0,0,44,44)]; // set the frame according to the image size
[leftButton setBackgroundImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(myActionMethod:) forControlEvents:UIControlEventTouchUpInside];
myLeftButton = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = myLeftButton;
Upvotes: 0
Reputation: 150565
You are passing the incorrect styles for the method you are using to create the button:
here:
[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonSystemItemAdd target:self action:@selector(settingsButtonTapped)];
you are using initWithImage:style:target:action
but the style value you are assigning of UIBarButtonSystemItemAdd
is not one of the allowable values. The documentation for the method says that it has to be one of UIBarButtonItemStyle
which are defined as. Try using one of these instead to see if you can get the effect you are after.
typedef enum {
UIBarButtonItemStylePlain,
UIBarButtonItemStyleBordered,
UIBarButtonItemStyleDone,
} UIBarButtonItemStyle;
Upvotes: 1