I have a couple UIButtons, and in IB they\'re set to Aspect Fit, but for some reason they\'re always stretching. Is there something else you have to set? I tried all the d
This answer is based on @WernerAltewischer's answer.
To avoid having connect my button to an IBOutlet to execute the code on it, I subclassed UIButton's class:
// .h
@interface UIButtonWithImageAspectFit : UIButton
@end
// .m
@implementation UIButtonWithImageAspectFit
- (void) awakeFromNib {
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
@end
Now create a custom button in your xib, and set its image (not the background image):
Then, set its class:
You're done!
Instead of
your button's image aspect fit is now as expected: