0

There is the following problem: I use an UIImage, and when I create it programmatically without setting image size my image is stretching by vertical with some issues. Look at the screenshot:

enter image description here

My code is:

[backButton setBackButtonBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Also I have the image with the same name but prefix "@2x". What's the trouble it might be?

malcoauri
  • 11,904
  • 28
  • 82
  • 137
  • You know you can set the back button to have no text to give you just an arrow. – DogCoffee Jan 23 '14 at 09:11
  • It's problem not only for the back button. It appears for each image created programmatically – malcoauri Jan 23 '14 at 09:14
  • 1
    see so answer http://stackoverflow.com/questions/7101608/setting-image-for-uibarbuttonitem-image-stretched – Deepesh Jan 23 '14 at 09:44
  • self.youuttonr.contentMode = UIViewContentModeScaleAspectFit; – Arun Jan 23 '14 at 10:45
  • @Deepesh It's a better idea to vote to close the question as a duplicate rather than just linking to another SO post that answers the question. – Abizern Jan 23 '14 at 10:47

3 Answers3

4

Try set Back Indicator Image for UINavigationBar, not setBackButtonBackgroundImage for UIBarButtonItem

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back_btn.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back_btn.png"]];

The color of the image is controlled by the tintColor property.

Or use

[[UIImage imageNamed:@"back_btn.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
kolesnikovakate
  • 209
  • 1
  • 3
  • 5
0

It might be due to the large height of you UIbutton , try it by decreasing button's size.

Saurav Mac
  • 82
  • 5
-1

I would try adding an UIImageView as a subview to the button and setting image on the UIImageView

  UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Phone.png"]];
[imageView setFrame:CGRectMake(0, 0, button.frame.size.width, button.frame.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[button addSubview:imageView];
Aravind G S
  • 392
  • 3
  • 17