2

I have added custom UIbarButtonItems on toolbar. Everything is created through NIB file. I see the images are stretched. My images are 20x20 and 40x40 for retina. They are png files and I ensured they dont contain any drop shadow and use anti-aliasing. Currently the toolbar looks like this. enter image description here

If you see, the same images appear sharp when not added as a barbuttonItem but just as imageview. How can I fix this.

Ankur
  • 5,086
  • 19
  • 37
  • 62
inspeero
  • 75
  • 5
  • Check out this answer. http://stackoverflow.com/questions/7101608/setting-image-for-uibarbuttonitem-image-stretched – Yan Jul 15 '13 at 14:13

1 Answers1

1

Could you try to create your UIBarButtonItems in your viewDidLoad method?

Here is how I do with a UINavigationBar and all images looks fine:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *rightButtonImage = [UIImage imageNamed:@"img1.png"];
[rightButton setImage:rightButtonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0.0, 7.0, rightButtonImage.size.width, rightButtonImage.size.height);
[rightButton addTarget:self action:@selector(method1) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];


UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *leftButtonImage = [UIImage imageNamed:"img2.png"];
[leftButton setImage:leftButtonImage forState:UIControlStateNormal];
leftButton.frame = CGRectMake(10.0, 7.0, leftButtonImage.size.width, leftButtonImage.size.height);
[leftButton addTarget:self action:@selector(method2) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];

method1 and method2 are void methods: -(void)method

and to add 2 buttons on the right side, I do something like this:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *rightButtonImage = [UIImage imageNamed:@"img3"];
[rightButton setImage:rightButtonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0.0, 7.0, rightButtonImage.size.width, rightButtonImage.size.height);
[rightButton addTarget:self action:@selector(method3) forControlEvents:UIControlEventTouchUpInside];

UIButton *middleRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *middleRightButtonImage = [UIImage imageNamed:@"img4"];
[middleRightButton setImage:middleRightButtonImage forState:UIControlStateNormal];
middleRightButton.frame = CGRectMake(0.0, 7.0, middleRightButtonImage.size.width, middleRightButtonImage.size.height);
[middleRightButton addTarget:self action:@selector(method4) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:[[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease], [[[UIBarButtonItem alloc] initWithCustomView:middleRightButton] autorelease], nil];

For UIToolbar you have to add items like this:

[toolbar setItems:[NSArray arrayWithObject:item1, item2, item3, item4, nil]];
Jeremy
  • 1,461
  • 12
  • 26
  • 1
    Thanks for the help. The images I had where not designed to be used with Interface builder and directly as a barbuttonItem. Going your route it worked! – inspeero Jul 17 '13 at 08:26