sakoaskoaso
sakoaskoaso

Reputation: 347

Swift resize image inside imageview

I have got this code in Swift

let image: UIImageView = {
    let v = UIImageView()
    v.image = #imageLiteral(resourceName: "st")
    v.contentMode = .scaleAspectFit
    v.frame = CGRect(x:0,y:0,width:100,height:100)
    v.translatesAutoresizingMaskIntoConstraints = false
    return v
}()
    image.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    image.topAnchor.constraint(equalTo: view.centerYAnchor,constant:-170).isActive = true

My goal is to have a UIImageView with width 100 and height 100, containing an image inside of it with the same measures. But my image is definitely bigger than 100px. Even when I change frame to 10px the image's size doesn't change.

Upvotes: 1

Views: 619

Answers (1)

DonMag
DonMag

Reputation: 77690

Have you tried this?

    image.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
    image.heightAnchor.constraint(equalToConstant: 100.0).isActive = true

Or, am I misunderstanding your question...

Upvotes: 1

Related Questions