Swift UIButton image has incorrect insets when using adjustsFontSizeToFitWidth

Viewed 131

I have a UIButton with image. When text is not scaled everything is ok. But when text started scaling with adjustsFontSizeToFitWidth, image edge insets are ignored and every time I see my image on the random position. My code is:

let tintedImage = UIImage(named: icon)?.withRenderingMode(.alwaysTemplate)
let tintedImageHighlighted = UIImage(named: icon)?.alpha(0.7).withRenderingMode(.alwaysTemplate)

button.setImage(soundTintedImage, for: .normal)
button.setImage(soundTintedImageHighlighted, for: .highlighted)

button.titleLabel?.adjustsFontSizeToFitWidth = true
button.titleLabel?.baselineAdjustment = .alignCenters
button.titleLabel?.minimumScaleFactor = 0.4

let imageInsets : UIEdgeInsets = UIEdgeInsetsMake(4, 18, 0, 0)
button.imageEdgeInsets = imageInsets

I am using auto layout and positioned UIButton centrally on the view and set leading and trailing constraints to let work adjustsFontSizeToFitWidth. Any ideas how to fix it?

0 Answers
Related