I have a UIButton
with constraints of 64x64px and a larger child ImageView
(155px) that does not scale to fit within the 64x64 parent button. Relevant properties:
UIButton
is of typecustom
UIButton
contentVerticalAlignment
andcontentHorizontalAlignment
is set tofill
UIButton
content mode isscaleToFill
- Child
ImageView
's content mode is set toscaleAspectFit
like below (all above properties I set with the attributes pane in interface builder):
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.imageView?.contentMode = .scaleAspectFit
if let imageView = button.imageView {
print("There is an image view", imageView.contentMode.rawValue) // Prints 1 since we set scale aspect fit above
} else {
print("There is NO image view")
}
}
// Rest of class, truncated for brevity
}
There are many other questions posted on SO (like UIButton doesn't listen to content mode setting?) and I've tried all the solutions with no luck.
I'm running Xcode version 13.0, not sure if something has changed in recent versions of Swift/Xcode or if I'm just missing something simple. Would really appreciate any ideas from the SO community. Thanks!