简体   繁体   中英

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?

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

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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM