How to resize an Image of UIButton in Swift?
bySometime there may be need to resize the image displayed in UIButton for not availability of proper sized image because of some reason. Wrong size gives unwanted results like stretched out image pixels etc. In this post we are assuming simple UIButton with image only.
First solution:
We can update contentMode settings of imageView property of UIButton. Default value is .scaleToFill. It scales the image to fill the UIButton ignoring aspect ratio.
btn.imageView?.contentMode = .scaleAspectFit
This will solve the 50% of problem because UIButton will render the image maintaining the aspect ratio.
Now if in worst case scenario problem isn’t solved because of bigger dimension of image then we have second solution: