0

is there any way I can set the size of an image in my NavigationBar programatically instead of changing in manually?

Here is the code which creates the button:

UIBarButtonItem *Share = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fb_share.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(Share:)];

My images size is 256*256 so it simply show up in this resolution and whole screen is messed.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
TomasJ
  • 289
  • 8
  • 21

2 Answers2

3

You just want to change the size of your image? If so, something like this should work:

// Given a UIImage image and a CGSize newSize:

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Then use newImage for the image of the UIBarButtonItem.

Marcel Puyat
  • 325
  • 3
  • 8
  • 1
    You have to first declare your UIImage obviously, and also set a CGSize newsize. But that should work if I'm not mistaken. Also, this might help if you still have trouble: http://stackoverflow.com/questions/6435118/how-set-images-on-buttons-of-navigation-bar?rq=1 – Marcel Puyat Nov 07 '13 at 08:58
3

Normaly you should use contentMode (in this case UIViewContentModeScaleAspectFit) every UIView subclass have this property usually you might use it to UIImageView. But i'm almost sure that does not work with UIBarButtonItem. Try, but you might hit a wall.

If my prediction was true you have to resize image programatically:

UIImage *myImage = [UIImage imageNamed:@"fb_share"];
float maximumHeight = 44;
float ratio = maximumHeight/ myImage.size.height;
CGSize imageNewSize = CGSizeMake(myImage.size.width*ratio, maximumHeight);
UIGraphicsBeginImageContextWithOptions(imageNewSize, NO, 0.0);
[myImage drawInRect:CGRectMake(0, 0, imageNewSize.width, imageNewSize.height)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *Share = [[UIBarButtonItem alloc] initWithImage:resizedImage style:UIBarButtonItemStyleBordered target:self action:@selector(Share:)];
Jakub
  • 13,712
  • 17
  • 82
  • 139
  • yes but this has just adjusted the height of image. Once I try to adjust maximumWidth the error pops up: redefinition of ratio. – TomasJ Nov 07 '13 at 09:06
  • Ooo i made a mistake. Change third line from `float ratio = myImage.size.height/maximumHeight;` to `float ratio = maximumHeight/ myImage.size.height;` – Jakub Nov 07 '13 at 09:09
  • If you have redefinition error you should look for this redefinition (you declare the same variable name twice). – Jakub Nov 07 '13 at 09:10
  • But there already is `float ratio = maximumHeight/ myImage.size.height;` – TomasJ Nov 07 '13 at 09:12
  • Yes, now i edit my answer. change it, not add it. If you have redefinition error, like i said you have variable ratio declared twice. Change name or remove redefinition. – Jakub Nov 07 '13 at 09:15
  • Ok I've solved it with Marcel Puyat's solution already! Thank you – TomasJ Nov 07 '13 at 09:17