简体   繁体   English

如何以编程方式使用“viewWithTag”更改图像?

[英]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? 但它抛出一个错误“UIView类型的值没有成员图像”,任何关于如何做到这一点的线索?

viewWithTag will give UIView in return. viewWithTag将给予UIView作为回报。 But you need ImageView type. 但是你需要ImageView类型。 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 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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