Quentin Rth
Quentin Rth

Reputation: 305

UIButton ImageView contentmode doesn't work

I just started an app for iOS in Swift, I'm doing the design programmatically (I mean I'm not using storyboard)

I have a UIButton with an image inside, I would like the image to fill the entire button. Right now it just show the image in my button, but smaller than the button, center in top of the button frame.

My code is :

    let buttonLocation = UIButton(frame: CGRect(x: 0, y: 0, width: buttonRoundedSize, height: buttonRoundedSize))
    buttonLocation.setImage(UIImage(named: "locate_button"), for: .normal)
    buttonLocation.backgroundColor = .white
    buttonLocation.layer.cornerRadius = frame.width / 2
    buttonLocation.myExtension()

Here are the things I've tried (none of theses worked) :

    self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    self.autoresizingMask = .flexibleWidth
    self.autoresizingMask = .flexibleHeight
    self.imageView?.contentMode = .scaleAspectFill

If I just put the contentMode line it doesn't work either. The imageView doesn't return nil i've checked..

Thanks!

Upvotes: 0

Views: 1578

Answers (5)

Dhaval H. Nena
Dhaval H. Nena

Reputation: 4160

Try setting the below alignment properties and it will work.

    closeButton.contentHorizontalAlignment = .fill
    closeButton.contentVerticalAlignment = .fill
    closeButton.imageView?.contentMode = .scaleAspectFit

Upvotes: 1

Pavel Shorokhov
Pavel Shorokhov

Reputation: 4994

Answer on your question: https://stackoverflow.com/a/26263623/3047318

TLDR: set contentMode in viewDidLoad

Upvotes: 0

BhargavR
BhargavR

Reputation: 1133

Try using below code and remove other properties which are being set for this button :

let buttonLocation = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
        buttonLocation.setImage(UIImage(named: "cat"), for: .normal)
        buttonLocation.backgroundColor = .white
        buttonLocation.layer.cornerRadius = 50
        buttonLocation.imageView?.contentMode = .scaleAspectFill
        buttonLocation.clipsToBounds = true

        self.view.addSubview(buttonLocation)

Here make sure that the image which you are setting in the button is larger than the frame of the button. You can change frame and corner radius values in the above code. I just tried with the above code and getting below output.

enter image description here

You can comment if this is not your expected output.

Hope this helps.

Upvotes: 0

Samir Shaikh
Samir Shaikh

Reputation: 557

you need to use setBackgroundImage property instead of setImage.

buttonLocation.setBackgroundImage(UIImage(named: "locate_button"), for: .normal)

Upvotes: -1

PGDev
PGDev

Reputation: 24341

Set the contentMode of buttonLocation's imageView as scaleAspectFill or scaleToFill to cover the entire frame of button.

let buttonLocation = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 500))
buttonLocation.setImage(UIImage(named: "image"), for: .normal)
buttonLocation.backgroundColor = .gray
buttonLocation.layer.cornerRadius = buttonLocation.frame.width / 2
buttonLocation.imageView?.contentMode = .scaleAspectFill //Here........

Example:

enter image description here

Upvotes: 0

Related Questions