如何在UITableView单元格中的ImageView中添加大小和位置? [英] How add Size and Position to ImageView in UITableView Cell?

查看:129
本文介绍了如何在UITableView单元格中的ImageView中添加大小和位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableView单元格中添加了一个图像:

I added a image to my UITableView Cell:

cell.imageView!.image = UIImage(命名为: Default.png)

一切正常,显示图像。现在,我想为图像指定一个自定义的大小和位置,因此我可以使用以下代码尝试它:

Everything works, the image is displayed. Now I want to give a custom size and position for the image, so I try it with this code:

cell.imageView!.frame = CGRect(x:29,y:17,宽度:14,高度:13)

但是由于任何原因它都不起作用。

But for any reason it doesn't work. Any advices?

推荐答案

如果您的位置不会改变,您可以在 TableViewCell

If your position will not change You can create frame in your TableViewCell

class TableViewCell: UITableViewCell {

@IBOutlet weak var imageView: UIImageView!

override func layoutSubviews() {
    super.layoutSubviews()

    self.imageView?.frame = CGRect(x: 12, y: 23, width: 12, height: 100)
}
}

您可以在 tableview(cellForRowAt)但如果帧不会改变则不好,因为 cellForRowAt 在向上或向下滚动或加载单元格时运行。您只能在UITableViewCell中定义一次。

You can change frame in tableview(cellForRowAt) but If frame will not change its a bad because cellForRowAt run when scrolling up - down or loading cell . You can define in UITableViewCell once.

这篇关于如何在UITableView单元格中的ImageView中添加大小和位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆