Amod Gokhale
Amod Gokhale

Reputation: 2448

iphone UIBarButtonItem : setBackgroundImage - Stretched

We have added UIBarButtonItem using storyboard and at runtime on click we wanted to change its image ( functionality similar to add bookmark /remove bookmark).

Researched on stack overflow.. lot of folks recommended using UIButton inside UIBarbuttonItem.

Below code changes image but its stretched ( even original image is showing as stretched.

Can anyone guide us for changing image only using UIBarButton ( not using UIButton )?

 [_barButtonAddToFav setBackgroundImage:[UIImage imageNamed:@"ic_add_to_fav.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Upvotes: 2

Views: 4283

Answers (5)

ViJay Avhad
ViJay Avhad

Reputation: 2700

Dont use :setBackgroundImage for UIBarButtonItem, rather Initialise it with following syntax of initialization with Image

This will simply create button with image, that will never Stretched.

   self.navigationItem.hidesBackButton = true
   let backButton = UIBarButtonItem(image: UIImage(named: "ico-nav-prev"), style: .Plain, target: self, action: #selector(viewController.back(_:)))
   self.navigationItem.leftBarButtonItem = backButton

Upvotes: 1

Ali Beadle
Ali Beadle

Reputation: 4526

I was struggling with this recently, all the solutions seem convoluted but it turns out that this just works (at least if the original image allocated on the storyboard is the same size as the new image - not sure it would if they differ):

barButton.image = (UIImage(named: "NewImage"))

Upvotes: 2

anshul-systematix
anshul-systematix

Reputation: 416

UIBarButtonItem will stretch whole image to its bar button area.

To solve this problem, Drag an UIButton inside your UIBarButtonItem and set image on UIButton.

Set UIButton type as custom and all property as want to set for BarButton.

It not get stretched and also you'll be able to set width/height resolution same as your image have.

Hope its help. Please let me know if we have to go with another solution.

Upvotes: 4

user3182143
user3182143

Reputation: 9609

Also the another code(ANSWER) for your QUESTION

 // Change the appearance of back button
     UIImage *backButtonImage = [[UIImage imageNamed:@"button_back"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)];
     [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

 // Change the appearance of other navigation button
     UIImage *barButtonImage = [[UIImage imageNamed:@"button_normal"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
     [[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Upvotes: 0

user3182143
user3182143

Reputation: 9609

Amod you can try this coding.It will help you anyway.

UIImage *buttonImg = [UIImage imageNamed:@"myImg.png"];
UIImage *buttonGreyImg = [buttonImg stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[myButton setBackgroundImage:buttonGreyImg forState:UIControlStateNormal];

Upvotes: 0

Related Questions