iOS How to scale an image in UIButton.
Introduction
This article shows how to scale an image in UIButton.
When UIButton will be a large scale or small scale by Autolayout engine, an image it was contained in the UIButton is not correspond to the scale normally.
This article will introduce how to solve the problem.
Solution
To solve the proble, you need to set three propertiesas below.
button.imageView?.contentMode = .scaleAspectFit button.contentHorizontalAlignment = .fill button.contentVerticalAlignment = .fill
If you didn't set contentHorizontalAlignment and contentVerticalAlignment, the image would be scaled by image orinal size(content size).
Maybe in iOS in default, imageView conteined in UIButton wouldn't scale up larger than original image size.
So you need to set those values and to be scaled the imageView in UIButton.
And, if you want to make some margin for the image in UIButton.
Set an imageEdgeInsets as below
button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10);