0

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!

Quentin Rth
  • 178
  • 1
  • 15

5 Answers5

0

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

PGDev
  • 23,751
  • 6
  • 34
  • 88
0

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.

BhargavR
  • 1,095
  • 7
  • 14
  • It still doesn't work and my image size is higher than the unbutton size so I really don't understand why it doesn't work... – Quentin Rth May 27 '19 at 07:44
0

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

TLDR: set contentMode in viewDidLoad

Pavel Shorokhov
  • 4,485
  • 1
  • 35
  • 44
-1

you need to use setBackgroundImage property instead of setImage.

buttonLocation.setBackgroundImage(UIImage(named: "locate_button"), for: .normal)
Samir Shaikh
  • 547
  • 3
  • 9
-1

Maybe this can help you:

let buttonLocation = UIButton(type: .custom)
buttonLocation.frame = CGRect(x: 0, y: 0, width: buttonRoundedSize, height: buttonRoundedSize)
buttonLocation.setImage(UIImage(named: "locate_button"), for: .normal)
buttonLocation.imageView?.contentMode = .scaleAspectFill
buttonLocation.contentHorizontalAlignment = .fill
buttonLocation.contentVerticalAlignment = .fill
buttonLocation.backgroundColor = .white
buttonLocation.layer.cornerRadius = frame.width / 2
buttonLocation.clipsToBounds = true

You need to add contentHorizontalAlignment and contentVerticalAlignment properties and don't forget set clipsToBounds to true to see the corner raius effect.

riza milani
  • 163
  • 1
  • 7