Search code examples
iosuiimageviewuibuttonautolayout

Autolayout: Difference between a UIImageView and UIButton


In the screenshot you can see a UIImageView and a UIButton (both use the same image). The UIImageView's Content Mode is set to Scale to Fill. The UIButton uses the same image but no matter what I set its Content Mode to there is always empty space to the right of the image. I thought its Intrinsic Content Size would default to the size of the image but that obviously isn't the case.

What is the right way to specify that the button's width should be the width of its image? TIA

enter image description here


Solution

  • To get them to scale the same set the backgroundImage property of the UIButton rather than the image property. The image property is made to be displayed beside the title. The backgroundImage property will scale the entire bounds and the intrinsic size should match your UIImageView. Hope this helps and fixes your issue.