我需要在视图中分配宽度为17像素的所有UIImageViews,并将所有子视图分配给星形图像。
我正在尝试这个:
for v in view.subviews {
let view = v as! UIImageView
if view.frame.width == 17 {
view.image = UIImage (named: "star.jpg")
}
}
但它不起作用。
答案 0 :(得分:0)
希望您已将imageview添加到self.view作为子视图。
for subView in self.view.subviews {
if subView.isKindOfClass(UIImageView)
{
let subImgView = subView as! UIImageView
if subImgView.frame.width == 17 {
subImgView.image = UIImage (named: "star.jpg")
}
}
}
答案 1 :(得分:0)
我找到了解决方案:
for view in self.view.subviews as [UIView] {
if let img = view as? UIImage {
image.image = UIImage (named: "star.jpg")
}
}