如何以编程方式添加imageView的宽度/高度?

时间:2018-07-08 10:28:54

标签: swift uitableview uiimageview image-size

如何更改width/height中的UIImageView。我有很多图片带有不同的width/height,我希望它们以imageView的大小显示。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
    let city = nicosiaPlaces [indexPath.row]
    cell?.textLabel?.text = city
    cell?.imageView?.image = UIImage(named: city)
    cell?.imageView?.frame.size = CGSize(width: 10, height: 10)
    return cell!
}

我想使这一行代码起作用:

cell?.imageView?.frame.size = CGSize(width: 10, height: 10)

1 个答案:

答案 0 :(得分:0)

替换:

cell?.imageView?.frame.size = CGSize(width: 10, height: 10)

使用:

cell?.imageView?.translatesAutoresizingMaskIntoConstraints = false
cell?.imageView?.heightAnchor.constraint(equalToConstant: 10).isActive = true
cell?.imageView?.widthAnchor.constraint(equalToConstant: 10).isActive = true

您似乎希望cell是可选的,但是如果上述方法不起作用,请尝试删除问号。