firstButton is a UIButton of type Custom. I'm programmatically putting three of them across each cell of a table, thusly:
[firstButton setImage:markImage forState:UIControlStateNormal];
[firstButton setContentMode:UIViewContentModeScaleAspectFit];
[cell.contentView addSubview:firstButton];
Elsewhere, I'm telling it to clipToBounds. What I get is a crop of the center square of the image, rather than an aspect-scaled rendering of it. I've tried this lots of ways, including setting the mode property on firstButton.imageView, which also doesn't seem to work.
I also advice to have a look at the
adjustsImageWhenHighlighted
UIButton
property to avoid weird deformations of the image, when the button is pressed.If you're dealing with the UIButton's image (as opposed to it's backgroundImage, setting the contentMode on the UIButton itself or on its imageView has no effect (despite what other answers say).
Alternatively do this instead:
Or size your image accordingly.
OR just use a UIImageView (which properly respects contentMode) with a UITapGestureRecognizer attached to it, or a transparent UIButton on top of it.
Found a fix for this. Set the
adjustsImageWhenHighlighted
property ofUIButton
toNO
.Hope this helps. Feel free to comment below, I will follow up on any questions that you have.