UIButton image scale in AspectFit?

I want to add an image to UIButton, and also want to scale my image in accordance with UIButton (make the image smaller). Please show me how to do this.

This is what I tried, but it does not work:

  • Adding an image to a button and using setContentMode :
 [self.itemImageButton setImage:stretchImage forState:UIControlStateNormal]; [self.itemImageButton setContentMode:UIViewContentModeScaleAspectFit]; 
  • Creating a "stretch image":
 UIImage *stretchImage = [updatedItem.thumbnail stretchableImageWithLeftCapWidth:0 topCapHeight:0]; 
+86
ios objective-c cocoa-touch uikit uibutton
Jan 08
source share
16 answers

If you really want to scale the image, do it, but you must resize it before using it. Resizing it at runtime will simply lose processor cycles.

This is the category I use to scale the image:

UIImage + Extra.h

 @interface UIImage (Extras) - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize; @end; 

UIImage + Extra.m

 @implementation UIImage (Extras) - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize { UIImage *sourceImage = self; UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = targetSize.width; CGFloat targetHeight = targetSize.height; CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGPoint thumbnailPoint = CGPointMake(0.0,0.0); if (!CGSizeEqualToSize(imageSize, targetSize)) { CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if (widthFactor < heightFactor) scaleFactor = widthFactor; else scaleFactor = heightFactor; scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; // center the image if (widthFactor < heightFactor) { thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; } else if (widthFactor > heightFactor) { thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } // this is actually the interesting part: UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); if(newImage == nil) NSLog(@"could not scale image"); return newImage ; } @end 

You can use it in the size you need. For example:

 [self.itemImageButton setImage:[stretchImage imageByScalingProportionallyToSize:CGSizeMake(20,20)]]; 
+28
Jan 08 '10 at 4:00
source share

I had the same problem. Just install the ContentMode ImageView, which is inside the UIButton.

 [[self.itemImageButton imageView] setContentMode: UIViewContentModeScaleAspectFit]; [self.itemImageButton setImage:[UIImage imageNamed:stretchImage] forState:UIControlStateNormal]; 

Hope this helps.

+198
Jul 19 '10 at 13:22
source share

None of the answers here worked for me, I solved the problem with the following code:

 button.contentMode = UIViewContentModeScaleToFill; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill; 

You can also do this in the interface builder.

enter image description here

+96
Jul 15 '15 at 11:51
source share

The easiest way to programmatically set a UIButton image in aspect mode:

Swift

 button.contentHorizontalAlignment = .fill button.contentVerticalAlignment = .fill button.imageView?.contentMode = .scaleAspectFit 

Objective-c

 button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill; button.imageView.contentMode = UIViewContentModeScaleAspectFit; 



Note: You can change .scaleAspectFit (UIViewContentModeScaleAspectFit) to .scaleAspectFill (UIViewContentModeScaleAspectFill) to set the fill format

+47
Aug. 21 '15 at 16:01
source share

I had problems with the image not resizing proportionally, so I fixed it with cross-attachments.

 fooButton.contentEdgeInsets = UIEdgeInsetsMake(10, 15, 10, 15); 
+19
Jun 07 2018-12-12T00:
source share

This can now be done using the IB UIButton properties. The key is to set your image as a background, otherwise it will not work.

enter image description here

+14
Sep 09 '14 at 21:37
source share

Turning around to Dave’s answer, you can set the contentMode imageView button all in IB without code using runtime attributes:

enter image description here

  • 1 means UIViewContentModeScaleAspectFit ,
  • 2 means UIViewContentModeScaleAspectFill .
+14
Mar 03 '16 at 2:56
source share

If you just want to reduce the size of your button:

 yourButton.contentMode = UIViewContentModeScaleAspectFit; yourButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10); 
+8
Apr 18 '14 at 8:54
source share

The cleanest solution is to use Auto Layout. I lowered the priority of content compression in my UIButton and set the image (not the background image) through Interface Builder. After that, I added a couple of restrictions that determine the size of my button (rather complicated in my case), and it worked like a charm.

+4
Dec 16 '14 at 17:06
source share

I have a method that does this for me. the method takes uibutton and makes the image aspect suitable

 -(void)makeImageAspectFitForButton:(UIButton*)button{ button.imageView.contentMode=UIViewContentModeScaleAspectFit; button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentFill; button.contentVerticalAlignment=UIControlContentVerticalAlignmentFill; } 
+4
Sep 20 '15 at 8:50
source share

make sure you set the image to the Image property, but not to Background

+3
13 '11 at 10:00
source share

1 - clear the default button text (important)

2 - set alignment as image

3 - set content mode as image

enter image description here

+3
Feb 17 '19 at 8:23
source share

The background image can really be easily scaled. You just need to do something like this in a subclass of UIButton:

 - (CGRect)backgroundRectForBounds:(CGRect)bounds { // you'll need the original size of the image, you // can save it from setBackgroundImage:forControlState return CGRectFitToFillRect(__original_image_frame_size__, bounds); } // Utility function, can be saved elsewhere CGRect CGRectFitToFillRect( CGRect inRect, CGRect maxRect ) { CGFloat origRes = inRect.size.width / inRect.size.height; CGFloat newRes = maxRect.size.width / maxRect.size.height; CGRect retRect = maxRect; if (newRes < origRes) { retRect.size.width = inRect.size.width * maxRect.size.height / inRect.size.height; retRect.origin.x = roundf((maxRect.size.width - retRect.size.width) / 2); } else { retRect.size.height = inRect.size.height * maxRect.size.width / inRect.size.width; retRect.origin.y = roundf((maxRect.size.height - retRect.size.height) / 2); } return retRect; } 
+2
Sep 13 '13 at 9:15
source share

For Xamarin.iOS (C #):

  myButton.VerticalAlignment = UIControlContentVerticalAlignment.Fill; myButton.HorizontalAlignment = UIControlContentHorizontalAlignment.Fill; myButton.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit; 
0
Oct 25 '16 at 7:38
source share

Swift 5.0

  myButton2.contentMode = .scaleAspectFit myButton2.contentHorizontalAlignment = .fill myButton2.contentVerticalAlignment = .fill 
0
Jun 08 '19 at 19:52
source share

You just need to set the UIButton image content mode for the three events. -

 [cell.button setImage:[UIImage imageWithData:data] forState:UIControlStateNormal]; [cell.button setImage:[UIImage imageWithData:data] forState:UIControlStateHighlighted]; [cell.imgIcon setImage:[UIImage imageWithData:data] forState:UIControlStateSelected]; 

We have a code for three bcoz events when highlighting or selecting, if the button size is SQUARE and the image size is a rectangle, then it displays a square image during selection or selection.

I am sure this will work for you.

-one
Sep 07 2018-11-11T00:
source share



All Articles