button.imageView?.contentMode = .scaleAspectFit after setting the image, or resize the image to the target size before assigning it to the button.Why this happens
A UIButton manages its own internal UIImageView (accessible via imageView). When you call setImage(_:for:), the button assigns the image to that image view but does not forward the button’s contentMode property to the image view. The image view keeps its default contentMode of .scaleToFill, which stretches the image to fill the button’s bounds, ignoring the aspect ratio.
Setting button.contentMode = .scaleAspectFit on the button itself has no effect because UIButton doesn’t use that property for its image rendering. The fix is to target the internal image view directly.
How to fix it
1. Set the image view’s contentMode (recommended)
After setting the image, update the button’s imageView content mode. Do this once during configuration — for example in viewDidLoad, a custom initializer, or a configure() method.
import UIKit
final class MyViewController: UIViewController {
private let actionButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
configureButton()
}
private func configureButton() {
let image = UIImage(named: "icon-large")
actionButton.setImage(image, for: .normal)
// Key line: apply aspect fit to the internal image view
actionButton.imageView?.contentMode = .scaleAspectFit
// Optional: prevent the image from being squished when the button is smaller than the image
actionButton.imageView?.clipsToBounds = true
// Layout: give the button a fixed size or constraints
actionButton.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(actionButton)
NSLayoutConstraint.activate([
actionButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
actionButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
actionButton.widthAnchor.constraint(equalToConstant: 80),
actionButton.heightAnchor.constraint(equalToConstant: 80)
])
}
}
Why this works: The button’s imageView is a regular UIImageView. Setting its contentMode to .scaleAspectFit makes it scale the image proportionally to fit within the image view’s bounds, which match the button’s frame.
Tip: If you also set a background image via
setBackgroundImage(_:for:), that uses a separatebackgroundImageViewwhosecontentModedefaults to.scaleToFill. Adjust it the same way if needed:button.backgroundImageView?.contentMode = .scaleAspectFit.
2. Resize the image before assigning (alternative)
If you prefer not to rely on the internal image view — for example, when the button size changes dynamically and you want pixel-perfect control — resize the image to the exact target size beforehand. This avoids runtime scaling altogether.
extension UIImage {
/// Returns a new image scaled proportionally to fit within `targetSize`.
func scaledToFit(_ targetSize: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: targetSize)
return renderer.image { _ in
let aspectWidth = targetSize.width / size.width
let aspectHeight = targetSize.height / size.height
let scaleFactor = min(aspectWidth, aspectHeight)
let scaledSize = CGSize(width: size.width * scaleFactor,
height: size.height * scaleFactor)
let origin = CGPoint(x: (targetSize.width - scaledSize.width) / 2,
y: (targetSize.height - scaledSize.height) / 2)
draw(in: CGRect(origin: origin, size: scaledSize))
}
}
}
// Usage
let original = UIImage(named: "icon-large")!
let fitted = original.scaledToFit(CGSize(width: 80, height: 80))
actionButton.setImage(fitted, for: .normal)
// No need to touch imageView.contentMode
This approach uses UIGraphicsImageRenderer (available since iOS 10) which is faster and color-space safe compared to the older UIGraphicsBeginImageContextWithOptions.
How to prevent it
- Always set
button.imageView?.contentMode = .scaleAspectFitimmediately aftersetImage(_:for:)when using non-square images in square or differently-proportioned buttons. - If you create buttons in Interface Builder, select the button, open the Attributes Inspector, and do not expect the “Content Mode” dropdown to affect the image — it won’t. Set the content mode in code (e.g., in an
@IBOutletdidSetorawakeFromNib). - When using dynamic type or layout-driven button sizes, prefer the contentMode approach; it adapts automatically.
- For performance-critical scrolling lists (table/collection views), pre-resize images to the display size to avoid per-frame scaling.
FAQ
Does this work for background images too?
No. setBackgroundImage(_:for:) uses a separate backgroundImageView. Set button.backgroundImageView?.contentMode = .scaleAspectFit if you need aspect-fit behavior for background images.
Why doesn’t Interface Builder’s Content Mode setting work?
The Content Mode dropdown in the Attributes Inspector applies to the button itself (its UIView.contentMode), which only affects how the button’s background is drawn. It does not propagate to the internal imageView.
Can I animate the contentMode change?
Changing contentMode is not animatable. If you need an animated transition (e.g., switching between aspect fit and fill), cross-fade between two buttons or use a custom UIImageView inside a plain UIControl instead.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
