I'm trying to create a button for adding an image which has a small camera icon next to the text "add image".
I have managed to get this result with the following code:
let btn = UIButton(type: .system)
let img = UIImage(named: "camera")
btn.setImage(img, for: .normal)
btn.contentMode = .scaleAspectFit
btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
btn.imageEdgeInsets = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
stackView.addArrangedSubview(btn)
let btnHeight = NSLayoutConstraint(item: btn, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: 40)
NSLayoutConstraint.activate([btnHeight])
The problem is that the aspect ratio is not correct even though contentMode
is set to .scaleAspectFit
. If I increase the left
and right
values for the imageEdgeInsets
nothing changes.
Does anyone know what I'm missing?