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

问题描述:

我目前正在与笔尖一起工作,需要根据他们的标签更改基于国家编程的图像。我已经试过:如何以编程方式用“viewWithTag”更改图像?

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

但它抛出一个错误“类型的UIView值没有成员图像”,如何做到这一点的任何线索?

viewWithTag会给的UIView的回报。但是您需要ImageView类型。所以你需要明确地转换它并分配图像。

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

您必须投射视图。我在移动,但它是这样的:

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