Set contentMode for Image inside Button in Swift

400 Views Asked by At

I am trying to setImage inside my UIButton but the image appears smaller inside the button although there is "free-space" and I also set the contentMode.

Button:

let noteButton: UIButton = {
    let v = UIButton()
    v.setImage(UIImage(systemName: "pencil"), for: .normal)
    v.tintColor = UIColor.white
    v.imageView?.contentMode = .scaleAspectFit
    v.translatesAutoresizingMaskIntoConstraints = false
    return v
}()

Constraints:

noteButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
    noteButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
    noteButton.centerYAnchor.constraint(equalTo: itemView.centerYAnchor).isActive = true
    noteButton.leadingAnchor.constraint(equalTo: linkButton.leadingAnchor, constant: 50).isActive = true

Result:

enter image description here

1

There are 1 best solutions below

0
On

I fixed the issue. I just had to add these to lines:

v.contentHorizontalAlignment = .fill
v.contentVerticalAlignment = .fill