0

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 type custom
  • UIButton contentVerticalAlignment and contentHorizontalAlignment is set to fill
  • UIButton content mode is scaleToFill
  • Child ImageView's content mode is set to scaleAspectFit 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!

  • I found the answer in the linked question https://stackoverflow.com/questions/69439301/uibutton-image-behavior-changed-in-ios-15 above, just confirming that for anyone who lands here. Thanks so much whoever linked me to that, you're fantastic. – naansequitur Oct 22 '21 at 01:45
  • Okay, but how do you do this with code? There is no `UIButton.ButtonType` case called `default` – Adam Dec 18 '21 at 12:47

0 Answers0