Search code examples
iosswiftuiviewviewwithtag

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?


Solution

  • 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")
    }