SwiftyJD
SwiftyJD

Reputation: 5441

How to change an image with a "viewWithTag" programmatically?

I'm currently working with nibs need to change the images based on their tag programmatically based on country. I've tried:

vc.view.viewWithTag(8).image = UIImage(named:"image")

but it throws an error "Value of type UIView has no member image", any clue on how to do this?

Upvotes: 2

Views: 1031

Answers (2)

Cesare
Cesare

Reputation: 9419

You have to cast the view. I'm on mobile but it's something like this:

if let imageView = vc.view.viewWithTag(8) as? UIImageView {
 // Stuff here 
}

Upvotes: 1

Sivajee Battina
Sivajee Battina

Reputation: 4174

viewWithTag will give UIView in return. But you need ImageView type. So you need to explicitly cast it and assign the image.

if let imgView = vc.view.viewWithTag(8) as? UIImageView {
    imgView.image = UIImage(named: "image")
}

Upvotes: 4

Related Questions