KMI
KMI

Reputation: 258

How to scale UIButton without scaling image in Swift?

Can anyone help me scale a UIButton without scaling UIButton's imageView?

This code scale button and button's imageView. I want the button to be scaled without scaling the imageView.

button.transform = CGAffineTransform(scaleX: 3, y: 3)
button.backgroundColor = UIColor(red: 0, green: 126/255, blue: 229/255, alpha: 1)
button.layer.cornerRadius = $0.bounds.size.width / 2

Upvotes: 0

Views: 368

Answers (3)

AtulParmar
AtulParmar

Reputation: 4570

Try this code, And set content insets from storyboard.

button.backgroundColor = UIColor(red: 0, green: 126/255, blue: 229/255, alpha: 1)
button.imageView?.contentMode = .scaleAspectFit

enter image description here

Upvotes: 0

Vollan
Vollan

Reputation: 1915

I think the easiest solution is to create a UIImageView as a subview inside the button that you can control however you want.

Upvotes: 0

kalpesh
kalpesh

Reputation: 1287

You can set Image in button Image. will not scale but you can set button as you want. See in the image my button have 100*100 image and button have more size than image.

enter image description here

Upvotes: 1

Related Questions